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. -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]. >> 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. > -- 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.
