在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 ...
随机推荐
- Decentraleyes - Local emulation of Content Delivery Networks
Decentraleyes, 是一个本地化第三方库文件的浏览器插件,提供三十多种语言支持.大致原理如下: 保存常用的第三方库文件到本地,当打开的页面中需要加载的第三方库文件在本地有副本时,随即进行拦截 ...
- 用navicat手动删除了数据表的记录,再次写入的时候,怎么让id重新从1开始?
问:用navicat手动删除了mariadb数据表的记录,再次写入的时候,自增id会继续,不会从1开始. 比如,原来有10条记录,全部清空,再次写入数据,id会从11开始,怎么让他重新从1开始呢? 重 ...
- 用JQuery操作元素的style属性
可以直接利用css()方法获取元素的样式属性,JQuery代码如下: 1 $("p").css("color"); //获取p元素的样式颜色 无论color属 ...
- easyui的datebox控件如何只要年月不要日谢谢知道的说一下
例子2015-01 格式easyui-datebox 加上 data-options="formatter:myformatter,parser:myparser"function ...
- python第五十二课--自定义异常类
myexception.py ''' 实现自定义异常类: ''' class MyException(Exception): def __init__(self,msg): super().__ini ...
- Android的面向组件思想
http://blog.csdn.net/luoxinwu123/article/details/8019547 面向组件思想是在软件规模扩大,复杂度上升的背景下,以面向对象为基础而提出的一种软件设计 ...
- 最长公共子序列(模板 LCSL)
博客: https://www.cnblogs.com/sasuke-/p/5396843.html 模板 #include<iostream>#include<cstdio> ...
- 【转】MySQL 当记录不存在时insert,当记录存在时update
MySQL当记录不存在时insert,当记录存在时更新:网上基本有三种解决方法 第一种: 示例一:insert多条记录 假设有一个主键为 client_id 的 clients 表,可以使用下面的语句 ...
- virtualbox+vagrant学习-1-环境安装及vagrantfile的简单配置-Mac系统
在学习docker-machine时,发现需要使用到虚拟化平台,所以选择了使用virtualBox. 然后后面发现需要使用vagrant来在mac系统中作为虚拟机管理软件,所以下面将会简述我自己学习使 ...
- ethereum/EIPs-1193 Ethereum Provider JavaScript API 如metamask更新后的接口
eip title author discussions-to status type category created requires 1193 Ethereum Provider JavaScr ...