Clarification regarding getting a singleton instance in GUICE

2,958 views
Skip to first unread message

Karthik Krishnan

unread,
Apr 22, 2008, 5:53:48 PM4/22/08
to google-guice
As a GUICE newbie, I hope this is not a silly question but does the
class

public class Singleton {
private static Singleton instance = new Singleton();

private Singleton() {}

public static Singleton getInstance() {
return instance;
}
}

Is this the same as

@Singleton
public class SingletonWithGuice {

public class SingletonWithGuice() {}
}

and

Injector injector = Guice.createInjector(new Module());
SingletonWithGuice swg =
injector.getInstance(SingletonWithGuice.class);

Dhanji R. Prasanna

unread,
Apr 22, 2008, 6:06:39 PM4/22/08
to google...@googlegroups.com
They're very close but there are subtle differences. A singleton in Guice is created per Injector rather than per classloader in the first example. So if you created two injectors (with the same config) you would get two instances of the singleton.

Dhanji.

Alen Vrečko

unread,
Apr 22, 2008, 6:36:10 PM4/22/08
to google-guice
That is not the same.

If I am not mistaken,

your Singleton is Singleton per VM that is

Singleton.getInstance() == Singleton.getInstance(); // Doh:)

but Guice Singleton is Singleton per Injector that is

SingletonWithGuice firstInjectorSingleton =
Guice.createInjector().getInstance(SingletonWithGuice.class);
SingletonWithGuice secondInjectorSingleton =
Guice.createInjector().getInstance(SingletonWithGuice.class);

here firstInjectorSingleton != secondInjectorSingleton

hope that clears it up.

Cheers,
Alen

Alen Vrečko

unread,
Apr 22, 2008, 6:37:07 PM4/22/08
to google-guice
note to self: use the refresh button more often:)

Alen Vrečko

unread,
Apr 22, 2008, 6:55:04 PM4/22/08
to google-guice
What I like about this group, I always learn something new, static is
indeed per classloader:)

Karthik Krishnan

unread,
Apr 22, 2008, 7:57:28 PM4/22/08
to google-guice
That does help. Thanks!!

Karthik Krishnan

unread,
Apr 22, 2008, 8:04:39 PM4/22/08
to google-guice
On another note, does the following code also generate one instance
per injector?

public class MyModule implements Module {
public void configure(Binder binder) {
binder.bind(Service.class)
.to(ServiceImpl.class)
.in(Scopes.SINGLETON);
}
}

From Guice's user guide.

Dhanji R. Prasanna

unread,
Apr 22, 2008, 10:06:13 PM4/22/08
to google...@googlegroups.com, google-guice
Yes only one instance of key [Service] but not of key [ServiceImpl].
So if you only @Inject Service everywhere you will receive the same
instance.

Dhanji.

On 23/04/2008, at 10:04 AM, Karthik Krishnan <Krishn...@gmail.com>
wrote:

>

Keerthi K

unread,
Aug 16, 2017, 3:18:14 PM8/16/17
to google-guice
Hi,

I am trying to create a singleton instance per class loader instead of an injector. I am fairly new to google guice and i am wondering if we can create a singleton using google guice per class loader instead of an injector? 

Stephan Classen

unread,
Aug 16, 2017, 3:36:57 PM8/16/17
to google...@googlegroups.com
I don't understand what you are trying to achieve. Guice is all about injecting so you cannot have guice without an injector.

If you try to have Guice inject an instance depending on the current classloader you could try to write a scope which ensures one instance per classloader.

Stephan Classen

unread,
Aug 16, 2017, 3:36:57 PM8/16/17
to google...@googlegroups.com
I don't understand what you are trying to achieve. Guice is all about injecting so you cannot have guice without an injector.

If you try to have Guice inject an instance depending on the current classloader you could try to write a scope which ensures one instance per classloader.



Am 16. August 2017 21:18:13 MESZ schrieb Keerthi K <keerthi...@gmail.com>:
>Hi,
>
>I am trying to create a singleton instance per class loader instead of
>an
>injector. I am fairly new to google guice and i am wondering if we can
>create a singleton using google guice per class loader instead of an
>injector?
>
>On Tuesday, April 22, 2008 at 9:06:13 PM UTC-5, dhanji wrote:
>>
>> Yes only one instance of key [Service] but not of key [ServiceImpl].
>
>> So if you only @Inject Service everywhere you will receive the same
>> instance.
>>
>> Dhanji.
>>
>> On 23/04/2008, at 10:04 AM, Karthik Krishnan <Krishn...@gmail.com
>> <javascript:>>
>> wrote:
>>
>> >
>> > On another note, does the following code also generate one instance
>> > per injector?
>> >
>> > public class MyModule implements Module {
>> > public void configure(Binder binder) {
>> > binder.bind(Service.class)
>> > .to(ServiceImpl.class)
>> > .in(Scopes.SINGLETON);
>> > }
>> > }
>> >
>> > From Guice's user guide.
>> >
>> >
>> > On Apr 22, 4:57 pm, Karthik Krishnan <Krishnan.1...@gmail.com>
>wrote:
>> >> That does help. Thanks!!
>> >>
>> >> On Apr 22, 3:55 pm, Alen Vrečko <alen_vre...@yahoo.com> wrote:
>> >>
>> >>> What I like about this group, I always learn something new,
>static
>> >>> is
>> >>> indeed per classloader:)
>> > >
>>
>>
>
>--
>You received this message because you are subscribed to the Google
>Groups "google-guice" group.
>To unsubscribe from this group and stop receiving emails from it, send
>an email to google-guice...@googlegroups.com.
>To post to this group, send email to google...@googlegroups.com.
>Visit this group at https://groups.google.com/group/google-guice.
>To view this discussion on the web visit
>https://groups.google.com/d/msgid/google-guice/5f867715-692c-4c13-a7c5-3818b17dbe40%40googlegroups.com.
>For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages