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. CRM项目之stark组件(2)

    那么从今天开始呢,我们就要开始设计属于我们自己的admin组件,起个名字就叫stark吧(当然你愿意叫什么都可以). stark组件之四步走 仿照admin组件实现流程,stark组件要实现四件事情: ...

  2. Ajax进阶之原生js与跨域jsonp

    什么是Ajax? 两个数求和: 用Jquery和数据用json格式 viws函数: from django.shortcuts import render,HttpResponse # Create ...

  3. 敌兵布阵 HDU - 1166 (树状数组模板题,线段树模板题)

    思路:就是树状数组的模板题,利用的就是单点更新和区间求和是树状数组的强项时间复杂度为m*log(n) 没想到自己以前把这道题当线段树的单点更新刷了. 树状数组: #include<iostrea ...

  4. go标准库的学习-mime/multipart

    参考:https://studygolang.com/pkgdoc 导入方式: import "mime/multipart" multipart实现了MIME的multipart ...

  5. day22 Pythonpython random随机模块:略!!!本文os模块

    OS模块 用于提供系统级别的操作: os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径 os.chdir("dirname") 改变当前脚本工作目录:相 ...

  6. 树莓派设置NTP同步

    pi@raspberrypi:~ $ sudo timedatectl set-ntp true--------------启用NTPpi@raspberrypi:~ $ date           ...

  7. oracle12C 创建PDB

    1.根据数据库现有模板创建PDB CREATE PLUGGABLE DATABASE ssptrad ADMIN USER sspIDENTIFIED BY oracle roles=(dba) fi ...

  8. [转]Win7 + Ubuntu 18.04 LTS (Bionic Beaver)双系统安装方法

    这里介绍在win7的基础上,安装ubuntu 18.04 LTS,实现双系统启动. 首先,假设你已安装了windows 7系统. 一. 制作ubuntu U盘启动盘. 方法见http://blog.p ...

  9. 【Codeforces 499D】Name That Tune

    Codeforces 499 D 题意:给\(n\)个曲子,每个曲子每一秒有\(p_i\)的几率可以被猜出来,过了\(t_i\)秒肯定能被猜出来,猜完第\(i\)首歌立即播第\(i+1\)首,问\(T ...

  10. Android学习之基础知识四-Activity活动5讲(Activity的生命周期)

    一.返回栈 1.Android是通过任务(Task)来管理活动,一个任务就是一个返回栈内所有活动的集合. 2.返回栈是一个后进先出的数据结构,每启动一个新的活动,该活动就会覆盖原来的活动,位于栈顶位置 ...