在web.xml中配置监听器来控制ioc容器生命周期
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());
}
}
由下面的核心代码可知,创建容器后得到一个context对象
public WebApplicationContext initWebApplicationContext(ServletContext servletContext) {
//创建容器
if (this.context == null) {
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容器生命周期的更多相关文章
- 在web.xml中配置error-page
在web.xml中配置error-page 在web.xml中有两种配置error-page的方法,一是通过错误码来配置,而是通过异常的类型来配置,分别举例如下: 一. 通过错误码来配置error ...
- web.xml中配置log4j
1.将 commons-logging.jar和 log4j.jar加入你的项目中:2.在src/下创建log4j.properties|log4j.xml文件:3.在web.xml中配置log4j的 ...
- web.xml中配置Spring中applicationContext.xml的方式
2011-11-08 16:29 web.xml中配置Spring中applicationContext.xml的方式 使用web.xml方式加载Spring时,获取Spring applicatio ...
- web.xml 中配置了error-page但不起作用问题
问题: 在web.xml 中配置了 error-page,但是好像不起作用,就是跳转不到指定的页面. 配置信息如下: <!-- 400错误 --> <error-page> & ...
- struts2 在 Action 或 Interceptor 中获取 web.xml 中配置的 <context-param> 参数 (这是我的第一篇博文,哈哈。)
最近为了改一个问题,想加一个控制开关,就在web.xml 中配置了一个 <context-param> 参数,并在 Action 或 Interceptor 中获取参数值. 1.在 web ...
- 在哪个web.xml中配置welcome页面
是在tomcat的web.xml中配置,而不是在你的%web-project-root%/WEB-INF/web.xml中! 示例 <welcome-file-list> <welc ...
- 在web.xml中配置SpringMVC
代码如下 <servlet> <servlet-name>springMVC</servlet-name> <servlet-class>org.spr ...
- web.xml中配置启动时加载的servlet,load-on-starup
web.xml中配置启动时加载的servlet,load-on-starup 使用servlet来初始化配置文件数据: 在servlet的配置当中,<load-on-startup>1&l ...
- web.xml中配置spring.xml的三种方式
我们知道spring在web.xml中可以有三种方式来配置其xml路径:org.springframework.web.servlet.DispatcherServletorg.springframe ...
随机推荐
- arcgis api for javascript中使用proxy.jsp
当我们使用arcgis api for javascript 查询arcgis服务时,如果查询的参数很长时,通过get方式提交会查询不到结果,因为get方式提交的参数有长度限制,需要通过代理的方式使用 ...
- MessageQueue 相关概念
/** * Implements a thread-local storage, that is, a variable for which each thread * has its own v ...
- typeof与instanceof的区别是什么?
在javascript中,判断一个变量的类型可以用typeof (1) 数字类型.typeof返回的值是number.比如说:typeof(1),返回值是number (2) 字符串类型,typeof ...
- 监控nginx
vi nginx_status.sh #!/bin/bash HOST="127.0.0.1" PORT="9222" # 检测nginx进程是否存在 func ...
- Vue2 第三天学习
个人小总结:1年多没有写博客,感觉很多知识点生疏了,虽然工作上能解决问题,但是当别人问到某个知识点的时候,还是迷迷糊糊的,所以坚持写博客是硬道理的,因为大脑不可能把所有的知识点记住,有可能某一天忘了, ...
- python基础学习第五天
li=[1,2,33,-1,'dbssd',[4,5,6],{4:'rfw',5:'re'}]del(li[1])print(li)print(type(li))#访问元素print(li[0])pr ...
- A类——Dima and a Bad XOR
http://codeforces.com/contest/1151/problem/B 题意: 给一个矩阵,只要找到每一列的任意一个异或和大于0,就找到解并返回,输出TAK和位置,没找到就输出NIE ...
- ESP32 DAC
ESP32有两个DAC通道,通道1链接GPIO25, 通道2链接GPIO26; 当DAC设置为 “built-in DAC mode”的时候,I2S可以通过DAC发送数据: 使用示例: dac_out ...
- spring HibernateTemplate.save() 方法的自动提交问题
如题: service1: dao1.save(obj); //失败,应该给spring捕获,但没有,程序继续执行下去了. redisService.fun1(); //被执行 service2 ...
- 3.3《想成为黑客,不知道这些命令行可不行》(Learn Enough Command Line to Be Dangerous)——less即more
Unix提供了两个工具查看不止文件的头部和尾部.这个功能程序叫做more,但有种更强大的变异体叫做less(起初我认为这是玩笑).less这个程序是交互性地,所以很难在输出时捕获,但是仍然为大家提供了 ...