用一個類來存放applicationContext:
public class ContextHolder {
? private final static ContextHolder instance = new ContextHolder();
? private ApplicationContext ac;
? private ContextHolder() {
? }
? public static ContextHolder getInstance() {
??? return instance;
? }
? public synchronized void setApplicationContext(ApplicationContext ac) {
??? this.ac = ac;
? }
? public ApplicationContext getApplicationContext() {
??? return ac;
? } ?
}
然后寫一個servlet,繼承自org.springframework.web.context.ContextLoaderServlet,并配置web.xml,讓它在tomcat啟動時自動運行。然后在它的init方法中,加入如下的代碼:
WebApplicationContext context = WebApplicationContextUtils.
??? getWebApplicationContext(this.getServletContext());
ContextHolder.getInstance().setApplicationContext(context);
public class ContextHolder {
? private final static ContextHolder instance = new ContextHolder();
? private ApplicationContext ac;
? private ContextHolder() {
? }
? public static ContextHolder getInstance() {
??? return instance;
? }
? public synchronized void setApplicationContext(ApplicationContext ac) {
??? this.ac = ac;
? }
? public ApplicationContext getApplicationContext() {
??? return ac;
? } ?
}
然后寫一個servlet,繼承自org.springframework.web.context.ContextLoaderServlet,并配置web.xml,讓它在tomcat啟動時自動運行。然后在它的init方法中,加入如下的代碼:
WebApplicationContext context = WebApplicationContextUtils.
??? getWebApplicationContext(this.getServletContext());
ContextHolder.getInstance().setApplicationContext(context);