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

Alper Akture picture Alper Akture · Apr 11, 2013 · Viewed 29.9k times · Source

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.

Answer

codejanovic picture codejanovic · Mar 23, 2016

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