semeiskii / google-gin

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

@Singleton Not Respected When Binding Concrete Type #146

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Create a concrete class MyClass annotated with @Singleton
2. Bind the class within a GIN module with i.e. "bind(MyClass.class);"
3. Request two instances of the class from the injector.

What is the expected output? What do you see instead?
Expect to receive the same instance on the second call.  Instead two separate 
instances are returned.

What version of the product are you using? On what operating system?
GWT 2.2 and GIN 1.5.0

Please provide any additional information below.
I haven't reproduced this case in absolute isolation, but ran into it in my 
project.  By switching to "bind(MyClassInterface.class).to(MyClass.class);" the 
constructor was no longer called twice.

Original issue reported on code.google.com by dacc.exa...@gmail.com on 29 Mar 2011 at 5:24

GoogleCodeExporter commented 9 years ago
When you're binding the class in the module (why are you doing that anyhow? :) 
you override its default binding, i.e. the implicit binding that contains the 
@Singleton annotation. If you want to explicitly bind the class in a module you 
have to specify its scope (unless you want default), e.g. 
bind(MyClass.class).in(Singleton.class);.

Original comment by aragos on 29 Mar 2011 at 6:19

GoogleCodeExporter commented 9 years ago
Ah ok, makes sense!  I was binding a class mostly to have all of my 
dependencies visually organized in the module.  Thanks.

Original comment by dacc.exa...@gmail.com on 29 Mar 2011 at 6:48