why there is two instances of a singleton with Google Guice dependency injection framework

会有一股神秘感。 提交于 2019-12-07 07:11:38

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!