xiaodududu / google-guice

Automatically exported from code.google.com/p/google-guice
Apache License 2.0
0 stars 0 forks source link

Multibinder Bindings Don't Always Respect Singleton Scope #791

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Description of the issue:

When using the multibinder, if you use 
`binder.addBinding().to(B.class).in(Singleton.class);` it will not enforce the 
singleton nature on that object, but using the `@Singleton` annotation directly 
will. 

Steps to reproduce:
1. Create a Multibinder.newSetBinder instance with an interface.
2. Bind an implementation using `.in(Singleton.class)`. 
3. Bind another implementation annotated with `@Singleton`.
4. Inspect that the instances retrieved are not singletons in the cases where 
`.in(Singleton.class)` was used, but are in the cases where `@Singleton` was 
used.

Expected behavior:

- I'd expect for .in(Singleton.class) to work, to not be exposed, or to throw 
an exception.

The attached tests illustrate the problem: `getInstance_ofSet_ContainsB` and 
`getInstance_ofB_IsIdentical` both fail under both Guice 3.0 and Guice 
4.0-beta. 

Original issue reported on code.google.com by nachtr...@gmail.com on 11 Dec 2013 at 6:10

Attachments:

GoogleCodeExporter commented 9 years ago
Use a separate bind(B.class).in(Singleton.class) to make this work.  What is 
happening is that you're making linked binding a singleton, but not the 
underlying binding a singleton.

The same thing would happen if you did:
  bind(Foo.class).to(FooImpl.class).in(Singleton.class);
and then tried to compare injecting Foo vs injecting FooImpl.  Injecting Foo 
multiple times would be the same, but it'd be a different instance than 
FooImpl, because "Foo" is the singleton, not "FooImpl".

Original comment by sberlin on 11 Dec 2013 at 6:15

GoogleCodeExporter commented 9 years ago
(would be good if gissuebot would link to migrated issues - e.g 
https://github.com/google/guice/issues/791 )

Original comment by joseph.g...@gmail.com on 2 Mar 2015 at 10:01