eager singleton binding constructed 3 times?

709 views
Skip to first unread message

batkins

unread,
Oct 8, 2010, 9:26:37 AM10/8/10
to google-guice
I'm seeing a class bound with asEagerSingleton() constructed 3 times
(based on a log message in the single, default constructor). Why is
this the case? Do I really have 3 instances running around?

Thanks

Jason Winnebeck

unread,
Oct 8, 2010, 9:35:56 AM10/8/10
to google...@googlegroups.com
I'm not sure if this is your problem or not, but myself and a lot of other
people have gotten confused when starting with guice that bindings have
singleton scope, not classes. For example: if you bind A to X as singleton and
B to X as singleton you will actually get two instances of X, because you're
saying that A and B are singletons, not X. You need to bind A to X and B to X
*then* bind X as singleton if you want both A and B injections to share the
same X instance.

Jason

Willi Schönborn

unread,
Oct 8, 2010, 9:46:34 AM10/8/10
to google...@googlegroups.com
On 08/10/10 15:35, Jason Winnebeck wrote:
> I'm not sure if this is your problem or not, but myself and a lot of
> other people have gotten confused when starting with guice that
> bindings have singleton scope, not classes. For example: if you bind A
> to X as singleton and B to X as singleton you will actually get two
> instances of X, because you're saying that A and B are singletons, not
> X. You need to bind A to X and B to X *then* bind X as singleton if
> you want both A and B injections to share the same X instance.
That's an excellent point. I encountered another strange situation where
guice
instantiates a singleton multiple times:
In case the bootstrapping fails, guice tries to continue and collect
any error that occurs to produce that javac-like error message.
During that boot it may happen that your broken singleton is required
three times and guice attempts to create it three times.

batkins

unread,
Oct 8, 2010, 9:58:06 AM10/8/10
to google-guice
I'm only binding the class, e.g.
bind(EventManager.class).asEagerSingleton();

The EventManager.class also has a @Singleton annotation, but my
understanding is that the binding in the module config takes
precedence. In any case, shouldn't a singleton be instantiated just
once?

batkins

unread,
Oct 8, 2010, 9:59:40 AM10/8/10
to google-guice
I'm not seeing any exceptions or other error messages, and it's a very
simple construction, so I don't think it's failing. Is there a way to
get better information about the bootstrapping process (more logging,
etc)?

On Oct 8, 9:46 am, Willi Schönborn <w.schoenb...@googlemail.com>
wrote:

Stuart McCulloch

unread,
Oct 8, 2010, 10:07:23 AM10/8/10
to google...@googlegroups.com
On 8 October 2010 14:59, batkins <bnba...@gmail.com> wrote:
I'm not seeing any exceptions or other error messages, and it's a very
simple construction, so I don't think it's failing.  Is there a way to
get better information about the bootstrapping process (more logging,
etc)?

are you only creating one injector?  singletons are per-binding, per-injector

btw, an easy way to get a quick stack trace is to put "new Throwable().printStackTrace();" in your constructor - that could help

if you turn the JDK logging level to FINE you'll get some trace for the various startup phases (but not for every injection/create)

On Oct 8, 9:46 am, Willi Schönborn <w.schoenb...@googlemail.com>
wrote:
> On 08/10/10 15:35, Jason Winnebeck wrote:> I'm not sure if this is your problem or not, but myself and a lot of
> > other people have gotten confused when starting with guice that
> > bindings have singleton scope, not classes. For example: if you bind A
> > to X as singleton and B to X as singleton you will actually get two
> > instances of X, because you're saying that A and B are singletons, not
> > X. You need to bind A to X and B to X *then* bind X as singleton if
> > you want both A and B injections to share the same X instance.
>
> That's an excellent point. I encountered another strange situation where
> guice
> instantiates a singleton multiple times:
> In case the bootstrapping fails, guice tries to continue and collect
> any error that occurs to produce that javac-like error message.
> During that boot it may happen that your broken singleton is required
> three times and guice attempts to create it three times.
>
>
>
> > Jason
>
> > On 10/8/2010 9:26 AM, batkins wrote:
> >> I'm seeing a class bound with asEagerSingleton() constructed 3 times
> >> (based on a log message in the single, default constructor).  Why is
> >> this the case?  Do I really have 3 instances running around?
>
> >> Thanks

--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To post to this group, send email to google...@googlegroups.com.
To unsubscribe from this group, send email to google-guice...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-guice?hl=en.




--
Cheers, Stuart

Alen Vrečko

unread,
Oct 8, 2010, 4:32:15 PM10/8/10
to google-guice
Are you by any chance using Gin (Guice for GWT)?

Cheers
Alen
Reply all
Reply to author
Forward
0 new messages