0

I have a singleton class which has been adopted so that the super method requires now an additional parameter, in my example it is set to foobar.

public class MySingleton extends MySystemLoader       
{

private static final long serialVersionUID = -6053739985096001185L;
private static MySingleton SINGLETON = new MySingleton();

public static MySingleton getInstance() {
    return SINGLETON;
}

private MySingleton() {
    super( MySingleton.class, "foobar" );
}

}

My problem how can I inject this parameter to the this singleton class. The previous version of the private constructor looked like this

private MySingleton() {
     super( MySingleton.class );
}

To be more concrete my app will run on a tomcat server and I want to inject the context name of my application into this singleton, because this will extract some configuration properties with this name and such stuff. Would be a context listener the right choice but also then how can i inject the context name into the singleton?

Al Phaba
  • 6,545
  • 12
  • 51
  • 83
  • `MySingleton` does not have a superclass (other than `Object`), how can you call `super` with any parameters at all? – lexicore Apr 10 '18 at 17:07
  • You have a static instance of your singleton but want to pass a dynamic parameter (context name) to it? Somehow singleton seems to be a very poor choice here. The only ways I can imagine is some kind of setter which assigns context name to a field of the singleto instance. – lexicore Apr 10 '18 at 17:10
  • You need to add a new attribute in MySingleton class and create the constructor method with this parameter – thoai nguyen Apr 10 '18 at 17:14
  • Thanks for the answers I switched from Singleton to a Factory Implementation, with this I could solve my Problem. This SO was also helpful https://stackoverflow.com/questions/1050991/singleton-with-arguments-in-java/1051043#1051043 – Al Phaba Apr 11 '18 at 20:12

0 Answers0