Guice: how do you configure an @Provides and @Singleton in a module in this case?

对着背影说爱祢 提交于 2019-12-04 15:39:03

问题


I have a provider method in a module annotated with @Provides:

@Provides
public ChatServicePerformanceMonitor getChatServicePerfMon() {
  ...
}

and I have annotated my ChatServicePerformanceMonitor with @Singleton. In my code, where I use this instance, I can't have guice "passively" inject it, due to a framework I'm using that's constructing the enclosing class (it does not use Guice, so this is the only way I know of to get the reference):

chatServicePerfMon = injector.getInstance(ChatServicePerformanceMonitor.class);

It seems Guice does not respect the @Singleton annotation on my ChatServicePerformanceMonitor class. I get an instance for every call to injector.getInstance(ChatServicePerformanceMonitor.class).

Adding the @Singleton to the provider method seems to fix this:

@Provides @Singleton
public ChatServicePerformanceMonitor getChatServicePerfMon() {
  ...
}

Is that the expected behavior? It seems an @Singleton on the instance should be all I would need.


回答1:


If you're creating the ChatServicePerformanceMonitor like this:

@Provides
public ChatServicePerformanceMonitor getChatServicePerfMon() {
  return new ChatServicePerformanceMonitor();
}

then your class level @Singleton annotation will have no effect because Guice isn't creating the object, you are. Guice can only enforce scope on objects it creates. There's nothing wrong with adding @Singleton to your getChatServicePerfMon() method.

If you have a no-argument constructor (or an @Inject constructor) on the ChatServicePerformanceMonitor class and you remove your @Provides method then continual calls to the injector will return the same singleton.




回答2:


In the meantime this feature is available (tested with Guice 4.0).

@Provides methods may now be also annotated with @Singleton to apply scope. See https://github.com/google/guice/wiki/Scopes




回答3:


You could always just do the dead-simple approach:

private ChatServicePerformanceMonitor perfMon = null;

@Provides
public ChatServicePerformanceMonitor getChatServicePerfMon() {
  if (perfMon == null) {
    perfMon = new ChatServicePerformanceMonitor();
  }

  return perfMon;
}


来源:https://stackoverflow.com/questions/15957237/guice-how-do-you-configure-an-provides-and-singleton-in-a-module-in-this-case

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