On Fri, Sep 10, 2010 at 4:34 PM, Brian Pontarelli <[email protected]>wrote:
> I definitely see that the binding is to the interface but what it is doing
> under the hoods is creating only a single instance of MyImpl. Plus, if you
> look at it from the OO perspective, the interface isn't really a singleton
> since it is merely an interface. The singleton is the implementation....
>
> Plus, it seems as though if I create two bindings, one for the interface
> and one for the impl, I'll end up with two instances as singletons.
>
> I could put the @Singleton on the impl and that might work, but I'm not
> sure on that and would need to try it.
>
> I guess it really seems like Guice should handle this automatically for me
> since it is managing the constraint on MyImpl through MyInterface.
>
The statement "bind(X.class).to(Y.class).in(Scopes.SINGLETON);" is telling
Guice that you want a single instance when you request "X", not when you
request "Y". It could be possible that someone wants Z also bound to Y and
wants that in a different scope, or wants Y itself in a different scope.
Or, probably the more common example: if the binding statement was made
with annotations...
bind(X.class).annotatedWith(named("foo")).to(Y.class).asEagerSingleton()
and also
bind(X.class).annotatedWith(named("bar")).to(Y.class).asEagerSingleton().
You're telling Guice that you want a single X when you inject it with
@Named("foo") and another single one when you inject it with @Named("bar").
If you also added a bind(Y.class).asEagerSingleton(), then when you link
from X to Y (through your original binding statement), you'll still retrieve
the sole instance of "Y", since X is really a "linked binding" that just
forwards its requests to Y. (X still scopes the result as a singleton if
you bound it that way, and continues to return that scoped instance, but the
instance will be the same as the one Y created.)
Hopefully that actually made some sense and wasn't just rambling.
sam
>
> -bp
>
>
> On Sep 10, 2010, at 2:07 PM, Jason Winnebeck wrote:
>
> > I ran into this before. I haven't used asEagerSingleton but only
> singleton scope and I assume the behavior is the same.
> >
> > The singleton nature is only on the binding. So you defined MyInterface
> as a singleton, and said nothing about MyImpl.
> >
> > So injector.getInstance(MyInterface.class) ==
> injector.getInstance(MyInterface.class);
> >
> > But
> > injector.getInstance(MyImpl.class) != injector.getInstance(MyImpl.class);
> >
> > What you should do is define MyImpl as a singleton if you really only
> want one MyImpl. Then I believe it is irrelevant whether or not MyInterface
> is in Singleton because there's only one MyImpl it can get, but I normally
> define both in singleton.
> >
> > Jason
> >
> > On 9/10/2010 3:58 PM, Brian Pontarelli wrote:
> >> I have an project that is using 1.0 still and I found an odd behavior
> that I
> >> wanted to ask about and see if it is different in 2.0. The issue is when
> >> asking for different sides of a binding. Here's my binding code:
> >>
> >> bind(MyInterface.class).to(MyImpl.class).asEagerSingleton();
> >>
> >>
> >> The interesting thing is that these two lines of code return different
> instances:
> >>
> >> injector.getInstance(MyInterface.class);
> >> injector.getInstance(MyImpl.class);
> >>
> >>
> >> Is this expected behavior? If not, does this behavior exist in 2.0? If
> so, can
> >> someone explain to me why this is expected?
> >>
> >> Thanks,
> >> -bp
> >>
> >> --
> >> You received this message because you are subscribed to the Google
> Groups
> >> "google-guice" group.
> >> To post to this group, send email to [email protected].
> >> To unsubscribe from this group, send email to
> >> [email protected]<google-guice%[email protected]>
> .
> >> For more options, visit this group at
> >> http://groups.google.com/group/google-guice?hl=en.
> >
> > --
> > You received this message because you are subscribed to the Google Groups
> "google-guice" group.
> > To post to this group, send email to [email protected].
> > To unsubscribe from this group, send email to
> [email protected]<google-guice%[email protected]>
> .
> > For more options, visit this group at
> http://groups.google.com/group/google-guice?hl=en.
> >
>
> --
> You received this message because you are subscribed to the Google Groups
> "google-guice" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected]<google-guice%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/google-guice?hl=en.
>
>
--
You received this message because you are subscribed to the Google Groups
"google-guice" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/google-guice?hl=en.