views:

394

answers:

4

Guice Singletons are weird for me

First I thought that

IService ser = Guice.createInjector().getInstance(IService.class);
System.out.println("ser=" + ser);
ser = Guice.createInjector().getInstance(IService.class);
System.out.println("ser=" + ser);

will work as singleton, but it returns

ser=Service2@1975b59
ser=Service2@1f934ad

its ok, it doesnt have to be easy.

Injector injector = Guice.createInjector();
IService ser = injector.getInstance(IService.class);
System.out.println("ser=" + ser);
ser = injector.getInstance(IService.class);
System.out.println("ser=" + ser);

works as singleton

ser=Service2@1975b59
ser=Service2@1975b59

So i need to have static field with Injector(Singleton for Singletons)

how do i pass to it Module for testing?

A: 

After googling I found

If you now wanted to have one type instance per JVM, I suggest that you roll 
your own Scope implementation. Try to stay away from manual singletons: they 
are error prone and make testing your code harder. 

so guice doesnt support real singletons :(

01
Unless you use it the way it is intended to be used;-) You are not supposed, as mentioned by Jesse Wilson, to create several Guice Injectors, one is always enough for your whole application. If you create several Guice Injectors, it is likely you don't understand Guice at all.
jfpoilpret
I disagree, I watch a big presentation about Guice and they always create new injector for every component. They just make it hard to understand, good for them, bad for me.
01
Yea, the presentation stunk, didn't it?
nes1983
A: 

You really need to do a little more testing (and reading) before making wild, inaccurate accusations like that. I'm using Guice to manage my singleton instances just fine.

What do your module bindings look like? The singleton pattern in the GoF is simply a way of ensuring that there is one instance of a class, but the use of static makes it actually a bad choice. The need for a singleton is that there is only one instance of a class available while the application is running.

The following is all that is needed to create a singleton instance using Guice:

public class myModule extends AbstractModule {
    protected void configure() {
        bind(Service.class).to(ServiceImpl.class).in(Singleton.class);
    }
}

When you ask Guice to give you the instance, you'll get the same instance unless you do some unique scoping yourself. Per default, the singleton will be for the current VM instance.

gpampara
I disagree, I thought Guice statically hold references(since they always create new injector in the video on the page)
01
Fact is that that is not how Guice works. Guice is only involved in the process of object creation and wiring up your object dependency graph. Once that happens, it sorta disappears. There are no "static" singletons - it's just the same object reference passed around to the different instances that use it or need it. The static notion was a way of doing it using the language directly, DI addresses that "shortcoming" of the language by allowing the same thing, just without the cost of using static state.
gpampara
+5  A: 

When using Guice, you should create exactly one injector per JVM. Usually you'll create it in your application's entry point (ie. in your public static void main method). If you call createInjector multiple times in your application, you may be using Guice wrong!

Singletons are instance-per-injector. Note that this is not instance-per-JVM. The best way to obtain a reference to a singleton is to be injected with it. The other way is to ask the one and only injector instance to give it to you.

Jesse Wilson
A: 

If you have a standard classic servlet (i.e. no main()), how do you get hold of a single Injector? Do you need to use and old fashioned singleton? Is this chicken and egg situtaion? And no, I dont understand guice yet, Ive only been reading about it for 2 days. And have more questions than answers.

wingnut