jeudi 2 avril 2015

Using Java singleton pattern (static access to the singleton) with Spring

Consider this code:



public class A {
private static final A INSTANCE = new A();
public A getInstance() {
return INSTANCE;
}
private A() {}
public void doSomething() {}
}

// elsewhere in code
A.getInstance().doSomething();


How do I do the same when A requires a spring bean for construction? I don't want to inject A in each class that needs it, but want those classes to be able to access the singleton instance statically (ie, A.getInstance()).


Aucun commentaire:

Enregistrer un commentaire