用一个类来存放applicationContext:

 
  1. public class ContextHolder {  
  2.   private final static ContextHolder instance = new ContextHolder();  
  3.   private ApplicationContext ac;  
  4.   private ContextHolder() {  
  5.   }  
  6.   public static ContextHolder getInstance() {  
  7.     return instance;  
  8.   }  
  9.   public synchronized void setApplicationContext(ApplicationContext ac) {  
  10.     this.ac = ac;  
  11.   }  
  12.   public ApplicationContext getApplicationContext() {  
  13.     return ac;  
  14.   }    
  15. }  

然后写一个servlet,继承自org.springframework.web.context.ContextLoaderServlet,并配置web.xml,让它在tomcat启动时自动运行。然后在它的init方法中,加入如下的代码: 

 
  1. WebApplicationContext context = WebApplicationContextUtils.  
  2.     getWebApplicationContext(this.getServletContext());  
  3. ContextHolder.getInstance().setApplicationContext(context);  
评论
发表评论

您还没有登录,请登录后发表评论

dearwolf
搜索本博客
我的相册
E4ad347d-0c94-3e1b-b11c-cd60a6dbc8cc-thumb
sina
共 3 张
最近加入圈子
存档
最新评论