`
xinklabi
  • 浏览: 1560514 次
  • 性别: Icon_minigender_1
  • 来自: 吉林
文章分类
社区版块
存档分类
最新评论

JavaWeb中获得Spring的ApplicationContext

 
阅读更多

昨天帮同事处理一个网站访问量统计的问题,项目中使用的是SSH。大概就是写了一个HttpSessionListener,在sessionCreated方法中对访问量进行累加,但是这里面要用到spring管理的service bean进行业务和持久化操作,突然想不到如何获得Spring的上下文了,在网上搜了搜,给找到了,这个原来研究Spring加载过程时了解过的,后来给忘了,哎~~现在再做个记录吧~

其实关键是要获得启动项目的ServletContext,而只要能获得request或session就可以获得它了,下面是在session监听器中获得ServletContext的例子:

public void sessionCreated(HttpSessionEvent se){

      HttpSession session = se.getSession();

      ServletContext cxt = session.getServletContext();

     

      org.springframework.context.ApplicationContext.ApplicationContext appCtx =    org.springframework.web.context.support.WebApplicationContextUtils.getWebApplicationContext(cxt);

}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics