问题
I am developper in a compagny using Google Guice as dependency injection framework in a MVC architecture.
The system use a Singleton model. We just discover that there's two instances of the model in the project and this is problem.
I know that Singleton is never the better way, but I am not the project designer.
Google Guice is the only model initializer so that there is not "new".
Google Guice sometimes inject the model by fields injection and
sometime in the constructor itself.
But, the model is scoped only one time.
The binding is done as:
bind(Foo.class).to(FooImpl.class).in(Scopes.SINGLETON);
The class itself is not a singleton with the private constructor and all.
How can it be possible for Guice to create two instances if the binding is done only one time and if there is not new? Is field injection and constructor injection can conflict the singleton?
edit:
I can't show the code, but the initialization module binds a thousand of other Singleton classes and there is no problem so this module is working for sure.
回答1:
The problem here was the binding declaration.
We fixed it by replacing the binding declaration to:
bind(FooImpl.class).in(Scopes.SINGLETON);
bind(Foo.class).to(FooImpl.class);
来源:https://stackoverflow.com/questions/11001920/why-there-is-two-instances-of-a-singleton-with-google-guice-dependency-injection