Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

guice

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.

like image 323
Alper Akture Avatar asked Apr 11 '13 19:04

Alper Akture


People also ask

What does @singleton do Guice?

The @Singleton syntax is useful for annotating @Provides methods, or annotating the class itself (though I prefer to keep my scoping annotations inside modules).

What are Guice modules?

The Guice module helps you to inject Guice managed components into your play application. The injection points are defined by the upcoming @javax. inject. Inject annotation, which is bundled with play at the moment.

What is @named annotation in Guice?

Dependency Injection for Java Developers with Dagger & Guice Guice provides another way also to map bindings without creating a custom annoation. It allows so using @Named annotation.

What does @inject do in Guice?

Note that the only Guice-specific code in the above is the @Inject annotation. This annotation marks an injection point. Guice will attempt to reconcile the dependencies implied by the annotated constructor, method, or field.


2 Answers

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

like image 112
codejanovic Avatar answered Sep 17 '22 12:09

codejanovic


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.

like image 42
condit Avatar answered Sep 17 '22 12:09

condit