Spring--------web应用中保存spring容器
---恢复内容开始---
问题:在一个web应用中我使用了spring框架,但有一部分模块或组件并没有托管给Spring,比如有的可能是一个webservice服务类,如果我想在这些非托管的类里使用托管对象该怎么办呢,很自然的我们需要获得spring容器对象的引用ApplicationContext,我的想法是在服务启动后,想办法将ApplicationContext容器的应用保存到一个静态变量中,以后使用就简单了。
1)刚开始用的是spring+struts2,实力话spring用的是ContextLoaderListener,如下
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/conf/comm/applicationContext-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
这种情况下通过写一个listener,在web.xml配置在spring的实例化之后就行,在contextInitialized方法中保存spring容器
public class ApplicationContextListener implements ServletContextListener{
public void contextDestroyed(ServletContextEvent arg0) {
}
public void contextInitialized(ServletContextEvent event) {
C.wac = WebApplicationContextUtils.getWebApplicationContext(event.getServletContext());
System.out.println("存储spring 容器结束:ApplicationContext="+C.wac);
}
}
2)今天写一个新项目,想尝试一下spring mvc,边学边做,第一件事自然是将spring+struts2中的一些东西以spring mvc的方式映射过来,哪不会学哪,自然保存spring容器的引用成为了其中一项映射工作,因为spring mvc的初始化用的是一个servlet(DispatcherServlet),所以不可能再用listener来保存容器(实例化顺序listener -> filter -> servlet),刚开始想的是用servlet,通过配置<load-on-startup>控制servlet的实例化在spring的servlet之后,但不知为什么就是无法获得spring容器,一直报空指针异常。最后没办法只能放弃。只能换思路了,在网上找到一个ApplicationContextAware接口,实现该接口的托管对象在被实例化时spring会通过setApplicationContext方法将ApplicationContext对象传入这和struts2中SessionAware等的设计是一样的,测试可以获得spring容器对象的引用,如下(这里有点小知识,spring在启动时对于单例对象会直接实例化,所以配置该对象时不用设置Scope,采用默认即可)
@Component
public class InitComponent implements ApplicationContextAware{
@Override
public void setApplicationContext(ApplicationContext ac)
throws BeansException {
//存储spring容器引用
C.wac = ac;
}
}
3)通过实现ApplicationContextAware已能获得容器引用,但有一个问题,如果你希望在spring容器实例化完做点事,比如启动一个维护线程做点维护工作,该工作可能要用到spring容器里的东东。则上面的方法并不能保证你获得spring容器引用时spring容器已初始化完毕。进一步搜搜搜发现spring提供了一些事件监听
spring提供了内置的几类的事件:ContextClosedEvent 、ContextRefreshedEvent(spring实例化完) 、ContextStartedEvent(spring实例化之前) 、ContextStoppedEvent(spring销毁后) 、RequestHandleEvent
其中的ContextRefreshedEvent正式我们需要的,在spring容器启动完成后会触发ContextRefreshedEvent事件,监听方法如下,实现
ApplicationListener,泛型参数传ContextRefreshedEvent,对于其它事件的监听类似
@Component
public class InitListener implements ApplicationListener<ContextRefreshedEvent>{
@Override
public void onApplicationEvent(ContextRefreshedEvent e) {
WebApplicationContext wac = (WebApplicationContext)e.getApplicationContext();
//存储spring容器引用
C.wac = wac;
System.out.println(C.wac);
//存储配置文件路径
C.CONF_PATH = wac.getServletContext().getRealPath("/WEB-INF/conf");
System.out.println(C.CONF_PATH);
//初始化log4j
PropertyConfigurator.configure(C.CONF_PATH+"/comm/log4j.properties");
//启动系统缓存
C.wac.getBean(CacheManager.class).start();
}
}
---恢复内容结束---
Spring--------web应用中保存spring容器的更多相关文章
- Spring——Web应用中的IoC容器创建(WebApplicationContext根应用上下文的创建过程)
基于Spring-4.3.7.RELEASE Spring的配置不仅仅局限在XML文件,同样也可以使用Java代码来配置.在这里我使用XML配置文件的方式来粗略地讲讲WebApplicationCon ...
- 如何在web项目中配置Spring的Ioc容器
在web项目中配置Spring的Ioc容器其实就是创建web应用的上下文(WebApplicationContext) 自定义要使用的IoC容器而不使用默认的XmlApplicationContext ...
- web项目中 集合Spring&使用junit4测试Spring
web项目中 集合Spring 问题: 如果将 ApplicationContext applicationContext = new ClassPathXmlApplicationContext(& ...
- web环境中的spring MVC
1. web.xml文件的简单详解 在web环境中, spring MVC是建立在IOC容器的基础上,要了解spring mvc,首先要了解Spring IOC容器是如何在web环境中被载入并起作用的 ...
- 06_在web项目中集成Spring
在web项目中集成Spring 一.使用Servlet进行集成测试 1.直接在Servlet 加载Spring 配置文件 ApplicationContext applicationContext = ...
- web.xml中配置Spring中applicationContext.xml的方式
2011-11-08 16:29 web.xml中配置Spring中applicationContext.xml的方式 使用web.xml方式加载Spring时,获取Spring applicatio ...
- Spring Web MVC中的页面缓存支持 ——跟我学SpringMVC系列
Spring Web MVC中的页面缓存支持 ——跟我学SpringMVC系列
- Web.xml中自动扫描Spring的配置文件及resource时classpath*:与classpath:的区别
Web.xml中自动扫描Spring的配置文件及resource时classpath*:与classpath:的区别 一.Web.xml中自动扫描Spring的配置文件(applicationCont ...
- 如何在Web项目中配置Spring MVC
要使用Spring MVC需要在Web项目配置文件中web.xml中配置Spring MVC的前端控制器DispatchServlet <servlet> <servlet-name ...
随机推荐
- jQuery validate 的valid()方法一直返回true
1 调用$('#myForm').valid(),一直返回ture eg:html <form id="myForm"> <input class="f ...
- jquery 处理密码输入框(input type="password" ) 模仿placeholder
html <form method="post" action=""> <ul> <li> <span>邮箱&l ...
- struts2中的action访问web对象
Struts2的Action就是一个普通的POJO对象,它和Web对象request.response.session和application没有耦合在一起,这样便于单独测试Action,那么我们在A ...
- Unity-视图操作
按下 F 键使画面的视角移动到被选中对象的正面 视图场景摄像机(14年的时候,这个操作弄的头晕--) 旋转:Alt(Command)+左键拖拽 平移:Alt+Ctrl(Command)+左键拖拽 旋转 ...
- 2014年亚洲区域赛北京赛区现场赛A,D,H,I,K题解(hdu5112,5115,5119,5220,5122)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud 下午在HDU上打了一下今年北京区域赛的重现,过了5题,看来单挑只能拿拿铜牌,呜呜. ...
- activiti笔记三 Activiti问题重现
测试的时候出现一个异常: ContextLoader.initWebApplicationContext(308) | Context initialization failed org.spring ...
- 【Nutch2.2.1基础教程之2.1】集成Nutch/Hbase/Solr构建搜索引擎之一:安装及运行【单机环境】
1.下载相关软件,并解压 版本号如下: (1)apache-nutch-2.2.1 (2) hbase-0.90.4 (3)solr-4.9.0 并解压至/usr/search 2.Nutch的配置 ...
- poj 1699 Best Sequence
http://poj.org/problem?id=1699 题意:给你n个长度为L的序列,求包含这几个序列的最短长度. 先预处理每两个序列之间的关系,然后dfs枚举就行. #include < ...
- NOI十连测 第四测 T2
思路:线段树套可持久化treap,可持久化treap我还是第一次听说.. 改题的时候没看数据范围..乱开数组T_T #include<algorithm> #include<cstd ...
- LeetCode_Length of Last Word
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...