Spring源码情操陶冶-ContextLoaderListener
前言:通过实例结合源码的方式解读,其中涉及到的文件来自于博主的Github毕设项目wxServer
Note: Springboot应用不在本文章讨论范围
web.xml中启用Spring
在一般的web应用程序,我们倘若用到Spring的话,需要在web.xml中配置以下的信息来使一些容器,例如Tomcat、Jetty等来加载Spring
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:com/du/wx/resources/spring/springContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
Spring主要通过ContextLoaderListener类在应用启动时加载其服务
ContextLoaderListener的官方注释
查看某个类的重要功能最好是观察其的注释,ContextLoaderLinstener官方注释如下:
//父级启动类,可用ContextLoader启动和ContextCleanupListener来关闭Spring的根web应用上下文
Bootstrap listener to start up and shut down Spring's root {@link WebApplicationContext}.
Simply delegates to {@link ContextLoader} as well as to {@link ContextCleanupListener}.
//这里给出了提示,如果需要用到自定义log4j的配置的话,则ContextListener需要在Log4jConfigListener之后
<p>This listener should be registered after {@link org.springframework.web.util.Log4jConfigListener}
in {@code web.xml}, if the latter is used.
//从Spring3.1之后,注入根web应用上下文可通过 WebApplicationInitializer,容器在启动会加载此接口,但是有个要求是容器的Servlet版本必须是3.0+,对Tomcat来说必须是7.0.15版本以上
<p>As of Spring 3.1, {@code ContextLoaderListener} supports injecting the root web
application context via the {@link #ContextLoaderListener(WebApplicationContext)}
constructor, allowing for programmatic configuration in Servlet 3.0+ environments.
See {@link org.springframework.web.WebApplicationInitializer} for usage examples.
@author Juergen Hoeller
@author Chris Beams
@since 17.02.2003
@see #setContextInitializers
@see org.springframework.web.WebApplicationInitializer
@see org.springframework.web.util.Log4jConfigListener
重要的批注都在新增的注释上,并且我们可以发现,web.xml中的listener节点具有代码逻辑上的先写先加载的特点。
特别需要注意的是如果用户需要用到
Log4jConfigListener的话,则必须写在ContextLoaderListener的前面
ContextLoaderListener结构
public class ContextLoaderListener extends ContextLoader implements ServletContextListener{}
最应该关注的是ServletContextListenr接口,我们应该知道实现此接口的类会在应用启动的时候,自动的调用其接口方法contextInitialized(ServletContextEvent event);
在关闭应用时候则会调用其另外一个接口方法contextDestroyed(ServletContextEvent evet)用来关闭web上下文信息。
- 初始化
public void contextInitialized(ServletContextEvent event) {
//调用的是父类ContextLoader的方法,看出来这是启动的关键
initWebApplicationContext(event.getServletContext());
}
- 关闭
//与初始化相对
closeWebApplicationContext(event.getServletContext());
//使用ContextCleanupLister监听类来销毁ServletContext的springwork属性信息
ContextCleanupListener.cleanupAttributes(event.getServletContext());
这里我们简单的看下销毁属性的代码方法
Enumeration<String> attrNames = sc.getAttributeNames();
while (attrNames.hasMoreElements()) {
String attrName = attrNames.nextElement();
//筛选出专属spring的属性
if (attrName.startsWith("org.springframework.")) {
Object attrValue = sc.getAttribute(attrName);
//基本WebApplication都实现了DisposableBean接口,表明所有的Bean都是可以释放的
if (attrValue instanceof DisposableBean) {
try {
((DisposableBean) attrValue).destroy();
}
catch (Throwable ex) {
logger.error("Couldn't invoke destroy method of attribute with name '" + attrName + "'", ex);
}
}
}
}
记录总结
web.xml中的listener节点具有先写先加载的特点,类似于java代码中的顺序执行
Log4jConfigListener类加载必须在ContextLoaderListenr类之前
ContextLoaderListener启动Spring并生成Spring根web服务上下文则是通过其父类ContextLoader来实现的,其销毁也只会销毁具有
org.springwork前缀的属性ServletContext代表应用服务上下文,其可以读取
<context-param>级别的参数,而ServletConfig/FilterConfig则会读取其相应servlet节点/filter节点下的<init-param>参数web.xml中listener、servlet、filter执行顺序为listener>filter>servlet,同类型的执行顺序为listener满足先写先加载,servlet、filter则由
mapping节点的先后顺序加载
下节预告
Spring源码情操陶冶-ContextLoader
Spring源码情操陶冶-ContextLoaderListener的更多相关文章
- Spring源码情操陶冶-ContextLoader
前言-阅读源码有利于陶冶情操,本文承接前文Spring源码情操陶冶-ContextLoaderListener 静态代码块内容 ContextLoader在被主动调用的时候,会执行其的一个静态块,代码 ...
- Spring源码情操陶冶-AbstractApplicationContext#finishBeanFactoryInitialization
承接前文Spring源码情操陶冶-AbstractApplicationContext#registerListeners 约定web.xml配置的contextClass为默认值XmlWebAppl ...
- Spring源码情操陶冶-AbstractApplicationContext#registerListeners
承接前文Spring源码情操陶冶-AbstractApplicationContext#onRefresh 约定web.xml配置的contextClass为默认值XmlWebApplicationC ...
- Spring源码情操陶冶-AbstractApplicationContext#onRefresh
承接前文Spring源码情操陶冶-AbstractApplicationContext#initApplicationEventMulticaster 约定web.xml配置的contextClass ...
- Spring源码情操陶冶-AbstractApplicationContext#initApplicationEventMulticaster
承接前文Spring源码情操陶冶-AbstractApplicationContext#initMessageSource 约定web.xml配置的contextClass为默认值XmlWebAppl ...
- Spring源码情操陶冶-AbstractApplicationContext#initMessageSource
承接前文Spring源码情操陶冶-AbstractApplicationContext#registerBeanPostProcessors 约定web.xml配置的contextClass为默认值X ...
- Spring源码情操陶冶-AbstractApplicationContext#registerBeanPostProcessors
承接前文Spring源码情操陶冶-AbstractApplicationContext#invokeBeanFactoryPostProcessors 瞧瞧官方注释 /** * Instantiate ...
- Spring源码情操陶冶-AbstractApplicationContext#invokeBeanFactoryPostProcessors
阅读源码有利于陶冶情操,承接前文Spring源码情操陶冶-AbstractApplicationContext#postProcessBeanFactory 约定:web.xml中配置的context ...
- Spring源码情操陶冶-AbstractApplicationContext#postProcessBeanFactory
阅读源码有利于陶冶情操,承接前文Spring源码情操陶冶-AbstractApplicationContext#prepareBeanFactory 约定:web.xml中配置的contextClas ...
随机推荐
- 【知识整理】这可能是RxJava 2.x 最好的入门教程(一)
一.前言 RxJava 对大家而言肯定不陌生,其受欢迎程度不言而喻.而在去年的早些时候,官方便宣布,将在一段时间后不再对 RxJava 1.x 进行维护,而在仓库中另辟蹊径,开始对 RxJava 2. ...
- jquery移出select指定option
$("#selectLine option[value!='']").remove();
- php中有关合并某一字段键值相同的数组合并
<?php function combine($array,$start,$key,$newkey){ static $new; //静态变量 foreach($array as $k=> ...
- spring aop + xmemcached 配置service层缓存策略
Memcached 作用与使用 基本介绍 1,对于缓存的存取方式,简言之,就是以键值对的形式将数据保存在内存中.在日常业务中涉及的操作无非就是增删改查.加入缓存机制后,查询的时候,对数据进行缓存,增删 ...
- 微信小程序的开发环境搭建(Windows版本)
前言: 小程序是指微信公众平台小程序,小程序可以帮助开发者快速的开发小程序,小程序可以在微信内被便捷地获取和传播:是一种不需要下载安装即可使用的应用小程序,和原有的三种公众号是并行的体系.2017年1 ...
- script标签跨域的缺点
1,只支持GET,不支持其他例如:put,delete,post等 2,想拿到数据需要服务器端做出相应处理,必须在window域下面有对应的执行函数.例如:window.callbackHandler ...
- Vijos 1001 谁拿了最多奖学金
题目描述 某校的惯例是在每学期的期末考试之后发放奖学金.发放的奖学金共有五种,获取的条件各自不同: 1) 院士奖学金,每人8000元,期末平均成绩高于80分(>80),并且在本学期内发表1篇或1 ...
- *bzoj1083题解
题目: 城市C是一个非常繁忙的大都市,城市中的道路十分的拥挤,于是市长决定对其中的道路进行改造.城市C的道路是这样分布的:城市中有n个交叉路口,有些交叉路口之间有道路相连,两个交叉路口之间最多有一条道 ...
- 你要clean Android Studio project 么
原文:http://tekeye.uk/android/export-android-studio-project 如果嫌复制出来的项目太多,可以用文后的批处理删除一些文件,Android studi ...
- es6知识总结--3
es6知识总结--3 es6对咱们es3,es5的数据类型进行了升级下边说新APIs! js数据类型有Number.String .oject.Boolean.Null.Undefined六种数据类型 ...