首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

【转:在Servlet(可能Filter,或者Listener)中使用spring的IOC容器】

2013-12-09 
【转:在Servlet(或者Filter,或者Listener)中使用spring的IOC容器】转自http://www.softfz.com/jzx/2532/在se

【转:在Servlet(或者Filter,或者Listener)中使用spring的IOC容器】
转自http://www.softfz.com/jzx/2532/
在servlet或者filter或者Listener中使用spring的IOC容器的方法是:
WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext());
由于spring是注入的对象放在ServletContext中的,所以可以直接在ServletContext取出WebApplicationContext 对象:
WebApplicationContext webApplicationContext = (WebApplicationContext) servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
事实上WebApplicationContextUtils.getWebApplicationContext方法就是使用上面的代码实现的,建议使用上面上面的静态方法

热点排行