Hi, I would like to add my two cents :-)
If I know that a class will only be instantiated once or twice, I
usually prefer to declare the logger field as non-static.
I put in this category Stateless Session EJBs, servlets, filters,
listeners, JSPs, and any singleton classes I implement by myself.
Also, on webapps, I tend to use the ServletContext as a singleton
instance container, instead of using the singleton pattern, whenever I
can. The performance is a bit worse, but it can be reduced by storing a
local reference in the classes that access them, and I gain some
benefits too: no need to manually free resources on context
undeployment, and the ability to put those classes on the common or
shared directory of Tomcat, and still have separate instances for each
webapp using them.
I usually create a simple factory class like:
class XXXFactory {
private static XXX singleton = null;
private static Object lock = new Object();
public static XXX getInstance(ServletContext ctx) {
XXX instance;
synchronized (lock) {
if (ctx == null) {
if (singleton == null) singleton = new XXX();
instance = singleton;
} else {
instance = (XXX)ctx.getAttribute(XXX.class.getName());
if (instance == null) {
instance = new XXX();
ctx.setAttribute(XXX.class.getName(), instance);
}
}
}
return instance;
}
}
Another option is to use the commons discovery framework, but sometimes
I just prefer a simpler solution. :-)
HTH,
Rodrigo Ruiz
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]