If can't or don't want to use the servlet context, you could add a "ProxyContextLoaderListener" to your application:
Code:
package demo.spring
import javax.servlet.ServletContextEvent;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.WebApplicationContext;
public class ProxyContextLoaderListener extends ContextLoaderListener {
private static WebApplicationContext ctx;
@Override
public void contextInitialized(ServletContextEvent event) {
super.contextInitialized(event);
ctx = ContextLoader.getCurrentWebApplicationContext();
}
@Override
public void contextDestroyed(ServletContextEvent event) {
super.contextDestroyed(event);
ctx = null;
}
public static WebApplicationContext getCtx() {
return ctx;
}
}
The code that needs to access a bean created by ApplicationContext could be as follows:
Code:
...
MyClass cl = (MyClass) ProxyContextLoaderListener.getCtx().getBean(
"myBeanId");
...
In the web.xml file you specify the ProxyContextLoaderListener instead of the standard Spring ContextLoaderListener:
Code:
...
<listener>
<listener-class>
demo.spring.ProxyContextLoaderListener
</listener-class>
</listener>
...