Should Guice Providers with expensive member instances as be annotated with @Singleton?

Viewed 11287

Should Guice Providers be annotated with @Singleton? My justification: if the Provider is providing an object to other Singleton classes and the object itself is relatively expensive to create, then wouldn't it make sense to use a Singleton Provider that constructs the expensive object in its @Inject-marked constructor, store it as a member and just return that already-saved global variable in the getter? Something like this:

@Singleton
public class MyProvider extends Provider<ExpensiveObject> {
    private ExpensiveObject obj;

    @Inject
    public MyProvider() {
        /* Create the expensive object here, set it to this.obj */
    }

    @Override
    public ExpensiveObject get() {
        return obj;
    }
}


Update

Let me clarify a little bit more here. This is not about whether I should be using @Singleton or .in(Singleton.class). This has to do more with the "caching" of the created object.

Let's say that object creation required multiple RPCs to complete, such as deserializing JSON or making HTTP requests. This could take quite some time. If I am going to use this Provider to inject into classes multiple times, then doesn't it make sense to only create such an object once?

Also note that I must be able to use a Provider because I need to be able to inject into the Provider.

3 Answers
Related