5、整合关键-在web.xml中配置监听器来控制ioc容器生命周期 原因:

1、配置的组件太多,需保障单实例

2、项目停止后,ioc容器也需要关掉,降低对内存资源的占用。 项目启动创建容器,项目停止销毁容器。

利用ServletContextListener监控项目来控制。 Spring提供了了这样的监控器:

在web.xml配置监听器:

<!-- 监听项目的创建和销毁,依据此来创建和销毁ioc容器 -->
<!-- needed for ContextLoaderListener -->
<context-param>
<!-- 指定Spring配置文件位置 -->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param> <!-- Bootstraps the root web application context before servlet initialization -->
<!--项目启动按照配置文件指定的位置创建容器,项目卸载,销毁容器 -->
<listener>
<!-- 加载jar包:org.springframework.web -->
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

打开

ContextLoaderListener源码发现其实现了ServletContextListener,其中有两个方法,一个创建容器,一个销毁容器
public class ContextLoaderListener extends ContextLoader implements ServletContextListener {
//...
/**
* Initialize the root web application context.
*/
@Override
public void contextInitialized(ServletContextEvent event) {
initWebApplicationContext(event.getServletContext());
} /**
* Close the root web application context.
*/
@Override
public void contextDestroyed(ServletContextEvent event) {
closeWebApplicationContext(event.getServletContext());
ContextCleanupListener.cleanupAttributes(event.getServletContext());
} }
ContextLoader
初始化WebApplicationContext
由下面的核心代码可知,创建容器后得到一个context对象
public WebApplicationContext initWebApplicationContext(ServletContext servletContext) {
//创建容器
if (this.context == null) {
 //private WebApplicationContext context;就是我们的ioc容器
this.context = createWebApplicationContext(servletContext);
}
}

context;就是我们的ioc容器,context有很多方法,

由上图可知getCurrentWebApplicationContext()方法可以获取当前的ioc容器。

    public static WebApplicationContext getCurrentWebApplicationContext() {
ClassLoader ccl = Thread.currentThread().getContextClassLoader();
if (ccl != null) {
WebApplicationContext ccpt = currentContextPerThread.get(ccl);
if (ccpt != null) {
return ccpt;
}
}
return currentContext;
}
getCurrentWebApplicationContext()属于ContextLoader类的,因为属于static,所以监听器来控制ioc容器生命周期
代码如下:
private static ApplicationContext ioc =  ContextLoader.getCurrentWebApplicationContext();
 

在web.xml中配置监听器来控制ioc容器生命周期的更多相关文章

  1. 在web.xml中配置error-page

    在web.xml中配置error-page 在web.xml中有两种配置error-page的方法,一是通过错误码来配置,而是通过异常的类型来配置,分别举例如下: 一.   通过错误码来配置error ...

  2. web.xml中配置log4j

    1.将 commons-logging.jar和 log4j.jar加入你的项目中:2.在src/下创建log4j.properties|log4j.xml文件:3.在web.xml中配置log4j的 ...

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

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

  4. web.xml 中配置了error-page但不起作用问题

    问题: 在web.xml 中配置了 error-page,但是好像不起作用,就是跳转不到指定的页面. 配置信息如下: <!-- 400错误 --> <error-page> & ...

  5. struts2 在 Action 或 Interceptor 中获取 web.xml 中配置的 <context-param> 参数 (这是我的第一篇博文,哈哈。)

    最近为了改一个问题,想加一个控制开关,就在web.xml 中配置了一个 <context-param> 参数,并在 Action 或 Interceptor 中获取参数值. 1.在 web ...

  6. 在哪个web.xml中配置welcome页面

    是在tomcat的web.xml中配置,而不是在你的%web-project-root%/WEB-INF/web.xml中! 示例 <welcome-file-list> <welc ...

  7. 在web.xml中配置SpringMVC

    代码如下 <servlet> <servlet-name>springMVC</servlet-name> <servlet-class>org.spr ...

  8. web.xml中配置启动时加载的servlet,load-on-starup

    web.xml中配置启动时加载的servlet,load-on-starup 使用servlet来初始化配置文件数据: 在servlet的配置当中,<load-on-startup>1&l ...

  9. web.xml中配置spring.xml的三种方式

    我们知道spring在web.xml中可以有三种方式来配置其xml路径:org.springframework.web.servlet.DispatcherServletorg.springframe ...

随机推荐

  1. python 之 初识面向对象

    编程的两种范式 我们知道,程序 = 特定的语法+数据结构+算法 好像这个和我们熟知的小说有类似之处啊,小说 = 人物+背景+情节 写小说呢,都是有模板的,so,写程序也是一样,我们把这个“模板”叫做编 ...

  2. 【WebLogic】weblogic调优

    版权声明:本文为博主原创文章(原文:blog.csdn.net/clark_xu 徐长亮的专栏),未经博主同意不得转载. https://blog.csdn.net/u011538954/articl ...

  3. centos7升级内核版本

    本文转载http://blog.csdn.net/nciasd/article/details/51490146,大神非常厉害!!!!! 查看当前系统的内核版本 # uname -r 1.导入key ...

  4. [TJOI2018]教科书般的亵渎

    嘟嘟嘟 题面挺迷的,拿第一个样例说一下: 放第一次亵渎,对答案产生了\(\sum_{i = 1} ^ {10} i ^ {m + 1} - 5 ^ {m + 1}\)的贡献,第二次亵渎产生了\(\su ...

  5. Arduino IDE for ESP8266 (0) 官方API

    http://arduino-esp8266.readthedocs.io/en/latest/esp8266wifi/readme.html 0 简单的连接到WIFI #include <ES ...

  6. python中.py和.pyw文件的区别

    :本文为博主原创文章,未经博主允许不得转载. 以下是摘录自百度问题的答案: 严格来说,它们之间的不同就只有一个:视窗运行它们的时候调用不同的执行档案. 视窗用 python.exe 运行 .py ,用 ...

  7. JavaWeb界面在线配置代码生成器

    关于直接main方法运行生成代码可参考我的这篇文章:MP实战系列(六)之代码生成器讲解 在线配置主要参考jeesite和jeecg,gun等开源项目,但是与它们相比又有很多不同? 与jeesite相比 ...

  8. 工具 Sublime日志染色

    工欲善其事,必先利其器. Preferences -> Browse Packages...

  9. 【Codeforces 115D】Unambiguous Arithmetic Expression

    Codeforces 115 D 题意:给一个没有括号的表达式,问有多少种添加括号的方法使得这是一个合法的表达式?输入可能有正负号.加减乘除.数字. 思路1: 这是不能过的\(naive\)的\(dp ...

  10. 3分钟学会做智能插座(DIY)

    转载请注明:@小五义http://www.cnblogs.com/xiaowuyiQQ群:64770604 感谢博达科技提供的技术支持,博达科技新出了turnip智能插座,通过微信控制,实现了语音控制 ...