The statements: bind(OneOfEachAnnotation.class).annotatedWith(A.class).to(OneOfEachAnnotation.class).in(Singleton.class); bind(OneOfEachAnnotation.class).annotatedWith(B.class).to(OneOfEachAnnotation.class).in(Singleton.class);
are saying, "Create a binding '@A OneOfEachAnnotation' that to OneOfEachAnnotation' and make the @A OneOfEachAnnotation a singleton. Same thing with @B. Ignoring the @Singleton on OneOfEachAnnotation for a second (assuming it didn't exist), then this would have the behavior of: User injects *OneOfEachAnnotation* (unannotated) multiple times: a new one is created each time, because that binding is unscoped. User injects *@A OneOfEachAnnotation*, the first time it links down to the unannotated OneOfEach, sees it needs to be constructed, and constructs it. The second time the binding is already provisioned, so Scopes.SINGLETON doesn't go to the linked binding again. (Same thing with @B as @A.) When OneOfEachAnnotation has @Singleton on it, then Guice thinks that the unscoped value is _also_ a singleton. So when @A links to the unannotated, it will create the first instance. When @B links down the unannotated, Guice notices that the unannotated version, also a Singleton, has already been constructed, and returns that instance. (Hopefully that makes some sense.) sam On Thu, Aug 18, 2011 at 2:04 PM, glenviewjeff <[email protected]>wrote: > Thanks for pointing this out Sam. The code in my question does have the > @Singleton annotation on the OneOfEachAnnotation. Removing this fixed the > problem. > > It's not entirely clear to me why this causes it to behave as a "single" > singleton instead of respecting the annotation. If you are able to help me > understand why, I'd be grateful. > > -- > You received this message because you are subscribed to the Google Groups > "google-guice" group. > To view this discussion on the web visit > https://groups.google.com/d/msg/google-guice/-/vGTbS-WhFEUJ. > > 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.
