Here's an implementation, based on the ideas discussed in this post that I worked up.
It comes with the warning to my team members that it should only be used sparingly and that we should migrate towards wiring everything together with Spring's application context via ActionSupport.getWebApplicationContext(). We derived our base action class from ActionSupport.
Code:
import javax.servlet.ServletContextEvent;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.WebApplicationContextUtils;
/**
* Save the copy of the application context loaded at Servlet startup
*
* http://forum.springframework.org/viewtopic.php?t=1762&highlight=static+access+application+context
* http://forum.springframework.org/viewtopic.php?t=3224&highlight=static+access+application+context
* http://forum.springframework.org/viewtopic.php?t=2354&highlight=static+access+application+context
*
* @author Matthew McCullough (Ambient Ideas, LLC)
* @since Jun 23, 2005
*/
public class SpringStaticAppContextListener extends ContextLoaderListener {
private static ApplicationContext ac;
/**
* Store the web application context into a static member variable.
*/
public void contextInitialized(ServletContextEvent event) {
super.contextInitialized(event);
ac = WebApplicationContextUtils.getRequiredWebApplicationContext(event.getServletContext());
}
/**
* Release this class' static reference to the application context.
*/
public void contextDestroyed(ServletContextEvent event) {
ac = null;
super.contextDestroyed(event);
}
/**
* Get the singleton instance of the Application Context that was
* loaded for this web application.
*
* @return intialized ApplicationContext for this web app.
*/
public static ApplicationContext getApplicationContext() {
return ac;
}
}