1

I have a class that has been configured to be a Singleton, like this:

@Singleton
public class MySingleton {
  public MySingleton(MyDependency dependencyObj) {
    // ...
  }
}

Is there a way to get a new instance of MyDependency injected into the single instance of MySingleton each time Guice provides it to some other class? I can't make any changes to MySingleton (e.g., to make it something other than a Singleton)

  • 3
    If you were to inject something different in the constructor each time, then it wouldn't be a singleton anymore, because different instances would exist simultaneously – knittl Dec 02 '19 at 20:10
  • `public MySingleton(MyDependencyProvider depProvider) {}` then when you need a new one `depProvider.get()` is the preferred method for doing this, per the Guice docs. – kendavidson Jan 02 '20 at 13:51

0 Answers0