I get your doubt:
since Struts2 Actions are ThreadLocal
, and hence every action, on every request, creates an instance of its object, will a Singleton behave correctly ?
From your code it seems you are referring to the pre-1.5* Singleton:
public class Singleton {
private static final Singleton INSTANCE = new Singleton();
private Singleton() {}
public static Singleton getInstance() {
return INSTANCE;
}
}
The answer is yes:
private static final Singleton INSTANCE = new Singleton();
since static
"wins" over ThreadLocal
, and makes it unique (instantiated once, then shared).
*Note that if you are on Java EE >= 6, there are many ways to handle Singletons better than this, with CDI, EJB3, etc... eg @Singleton
EJB or @Singleton
injections.