---恢复内容开始---

问题:在一个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容器的更多相关文章

  1. Spring——Web应用中的IoC容器创建(WebApplicationContext根应用上下文的创建过程)

    基于Spring-4.3.7.RELEASE Spring的配置不仅仅局限在XML文件,同样也可以使用Java代码来配置.在这里我使用XML配置文件的方式来粗略地讲讲WebApplicationCon ...

  2. 如何在web项目中配置Spring的Ioc容器

    在web项目中配置Spring的Ioc容器其实就是创建web应用的上下文(WebApplicationContext) 自定义要使用的IoC容器而不使用默认的XmlApplicationContext ...

  3. web项目中 集合Spring&使用junit4测试Spring

    web项目中 集合Spring 问题: 如果将 ApplicationContext applicationContext = new ClassPathXmlApplicationContext(& ...

  4. web环境中的spring MVC

    1. web.xml文件的简单详解 在web环境中, spring MVC是建立在IOC容器的基础上,要了解spring mvc,首先要了解Spring IOC容器是如何在web环境中被载入并起作用的 ...

  5. 06_在web项目中集成Spring

    在web项目中集成Spring 一.使用Servlet进行集成测试 1.直接在Servlet 加载Spring 配置文件 ApplicationContext applicationContext = ...

  6. web.xml中配置Spring中applicationContext.xml的方式

    2011-11-08 16:29 web.xml中配置Spring中applicationContext.xml的方式 使用web.xml方式加载Spring时,获取Spring applicatio ...

  7. Spring Web MVC中的页面缓存支持 ——跟我学SpringMVC系列

    Spring Web MVC中的页面缓存支持 ——跟我学SpringMVC系列

  8. Web.xml中自动扫描Spring的配置文件及resource时classpath*:与classpath:的区别

    Web.xml中自动扫描Spring的配置文件及resource时classpath*:与classpath:的区别 一.Web.xml中自动扫描Spring的配置文件(applicationCont ...

  9. 如何在Web项目中配置Spring MVC

    要使用Spring MVC需要在Web项目配置文件中web.xml中配置Spring MVC的前端控制器DispatchServlet <servlet> <servlet-name ...

随机推荐

  1. Repeater里面加上if判断

    //创建时绑定 protected void ItemCreated(object sender, RepeaterItemEventArgs e) { if (e.Item.DataItem != ...

  2. Linux进程控制——exec函数族

    原文:http://www.cnblogs.com/hnrainll/archive/2011/07/23/2114854.html 1.简介 在Linux中,并不存在exec()函数,exec指的是 ...

  3. SQL Server根据列名查表

    select a.name, b.name from syscolumns a, sysobjects b where a.name = 'XXXX' and a.id = b.id and b.xt ...

  4. C# 广播TS流精确计时发送

    广播传输相关的项目,需求是UDP发送TS到IP/ASI网关,网关经过ASI输出到激励器,再由激励器通过射频天线输出,接收端为终端机顶盒. 因为以前没有怎么接触过广播相关的东西,一开始认为用C#写个UD ...

  5. VisualStudio2013快捷键

    visual studio 2013 是一个基本完整的开发工具集,它包括了整个软件生命周期中所需要的大部分工具,如UML工具.代码管控工具.集成开发环境(IDE)等等.VS 2013 中新增了很多提高 ...

  6. mybatis_Generator配置

    mybatis-generator-core-1.3.2 <?xml version="1.0" encoding="UTF-8"?> <!D ...

  7. 操作数据表中的记录——SELECT (where表达式、GROUP BY、HAVING、LIMIT)

    原文链接:http://www.ifyao.com/2015/01/26/%E6%93%8D%E4%BD%9C%E6%95%B0%E6%8D%AE%E8%A1%A8%E4%B8%AD%E7%9A%84 ...

  8. 高性能PHP日志插件--Seaslog

    日志系统作为记录系统运行的信息,包括 用户输入,安全日志等,日志系统是不能影响用户的使用. 为什么需要记录日志? 既然日志系统增加了整个系统的开销,为什么我还需要它,这是因为日志能帮我们记录运行的很多 ...

  9. ImageView一例

    参考自<疯狂android讲义>2.4节 效果如下: 当点击图上某点时,将之附近放大至下图. 布局文件: <LinearLayout xmlns:android="http ...

  10. SQL Server 索引的图形界面操作 <第十二篇>

    一.索引的图形界面操作 SQL Server非常强大的就是图形界面操作.关于索引方面也一样那么强大,很多操作比如说重建索引啊,查看各种统计信息啊,都能够通过图形界面快速查看和操作,下面来看看SQL S ...