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 ...
随机推荐
- 初始化CSS
为什么要初始化CSS? 建站老手都知道,这是为了考虑到浏览器的兼容问题,其实不同浏览器对有些标签的默认值是不同的,如果没对CSS初始化往往会出现浏览器之间的页面差异.当然,初始化样式会对SEO有一定的 ...
- Python学习:基本概念
Python学习:基本概念 一,python的特点: 1,python应用场景多;爬虫,网站,数据挖掘,可视化演示. 2,python运行速度慢,但如果CPU够强,这差距并不明显. 3,严格的缩进式编 ...
- Linux下Shadow socks的安装和配置
实在受不了在Windows下编程,所以自己就安装了一个Ubutun,公司用的FQ软件shadowsocks在Windows上用起来很简单很爽,但是在Ubutun上的安装和配置就没那么简单了,写下这篇文 ...
- Java8 Lambda/Stream使用说明
一.Stream流1. 流的基本概念 1.1 什么是流?流是Java8引入的全新概念,它用来处理集合中的数据,暂且可以把它理解为一种高级集合.众所周知,集合操作非常麻烦,若要对集合进行筛选.投影,需要 ...
- win7系统中使用DOS命令是出现乱码的解决方法
方法一:设置cmd显示字体1.win+R打开运行窗口->输入cmd->回车,打开命令行提示符窗口 win7系统运行窗口win7系统DOS命令行提示窗口 2.在命令行标题栏上点击右键,选择” ...
- mysql安装不上 failed to install the service
先前安装的没有卸载干净必须删除相应的注册表方法如下:1)“运行”中敲入“Regedit”进入注册表编辑2)HKEY_LOCAL_MACHINE->SYSTEM->ControlSet001 ...
- php对数组中的键与值进行合并处理
$res=array(); $re=array_count_values($month); foreach( $re as $k=>$v){ $arr['month_name'] = strva ...
- mysql查看数据表索引信息
查看索引 mysql> show index from tblname; mysql> show keys from tblname; · Table 表的名称. · Non_unique ...
- 【LeetCode】190. Reverse Bits
题目: Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented ...
- php文件的管理
一.先做一下简单的查看文件功能,文件中的文件和文件夹都显示,但是双击文件夹可以显示下一级子目录,双击"返回上一级"就可以返回到上一级目录 (1)先将需要管理的文件遍历出来,可以加个 ...