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 ...
随机推荐
- sql 参数
sqlserver :传参数是“@” oracle:是“:” mysql:是“?”
- json的js和C#操作
C#端的WebService接口接收json格式数据,处理后以json格式返回result using System; using System.Collections.Generic; using ...
- IOS 手机端搜索硬件设备 --- 物联网
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<errno.h> #i ...
- (转)ultraedit for linux 安装与注册破解
http://hi.baidu.com/simonwoos_zone/item/93f88b41dbad48e41e19bcc8
- 图的割点 桥 双连通(byvoid)
[点连通度与边连通度] 在一个无向连通图中,如果有一个顶点集合,删除这个顶点集合,以及这个集合中所有顶点相关联的边以后,原图变成多个连通块,就称这个点集为割点集合.一个图的点连通度的定义为,最小割点集 ...
- Asp.Net通过SignalR实现IM即时通讯
前言:SignalR是一种针对H5中WebSocket的解决方案,可以实现在不支持H5的浏览器中实现IM 后端: step 1:通过NuGet安装SignalR step 2:新建一个类继承于Hub, ...
- web 前端 shopnc项目 首页分类一开始做前端,我是拒绝的
看图别说话 经过几小时的折腾 主要还是靠耐心
- 几个STL算法:includes,set_difference、set_intersection、set_symmetric_difference、set_union, pre_permutation, next_permutation
includes: 测试有序序列中是否包含另一个序列的全部元素. template<class inputIterator1, class inputIterator2> bool inc ...
- NOI十连测 第四测 T2
思路:线段树套可持久化treap,可持久化treap我还是第一次听说.. 改题的时候没看数据范围..乱开数组T_T #include<algorithm> #include<cstd ...
- 懒猫们终究要付出代码(本领是一生的),鲸鱼们的短视(逐小利而暴死)——这么说我应该只去互联网公司:IM,云存储,邮箱(别的一概不考虑)
摘自周鸿伟的书,好像: