前言:通过实例结合源码的方式解读,其中涉及到的文件来自于博主的Github毕设项目wxServer

Note: Springboot应用不在本文章讨论范围

web.xml中启用Spring

在一般的web应用程序,我们倘若用到Spring的话,需要在web.xml中配置以下的信息来使一些容器,例如TomcatJetty等来加载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);
    }
    }
    }
    }

记录总结

  1. web.xml中的listener节点具有先写先加载的特点,类似于java代码中的顺序执行

  2. Log4jConfigListener类加载必须在ContextLoaderListenr类之前

  3. ContextLoaderListener启动Spring并生成Spring根web服务上下文则是通过其父类ContextLoader来实现的,其销毁也只会销毁具有org.springwork前缀的属性

  4. ServletContext代表应用服务上下文,其可以读取<context-param>级别的参数,而ServletConfig/FilterConfig则会读取其相应servlet节点/filter节点下的<init-param>参数

  5. web.xml中listener、servlet、filter执行顺序为listener>filter>servlet,同类型的执行顺序为listener满足先写先加载,servlet、filter则由mapping节点的先后顺序加载

下节预告

Spring源码情操陶冶-ContextLoader

Spring源码情操陶冶-ContextLoaderListener的更多相关文章

  1. Spring源码情操陶冶-ContextLoader

    前言-阅读源码有利于陶冶情操,本文承接前文Spring源码情操陶冶-ContextLoaderListener 静态代码块内容 ContextLoader在被主动调用的时候,会执行其的一个静态块,代码 ...

  2. Spring源码情操陶冶-AbstractApplicationContext#finishBeanFactoryInitialization

    承接前文Spring源码情操陶冶-AbstractApplicationContext#registerListeners 约定web.xml配置的contextClass为默认值XmlWebAppl ...

  3. Spring源码情操陶冶-AbstractApplicationContext#registerListeners

    承接前文Spring源码情操陶冶-AbstractApplicationContext#onRefresh 约定web.xml配置的contextClass为默认值XmlWebApplicationC ...

  4. Spring源码情操陶冶-AbstractApplicationContext#onRefresh

    承接前文Spring源码情操陶冶-AbstractApplicationContext#initApplicationEventMulticaster 约定web.xml配置的contextClass ...

  5. Spring源码情操陶冶-AbstractApplicationContext#initApplicationEventMulticaster

    承接前文Spring源码情操陶冶-AbstractApplicationContext#initMessageSource 约定web.xml配置的contextClass为默认值XmlWebAppl ...

  6. Spring源码情操陶冶-AbstractApplicationContext#initMessageSource

    承接前文Spring源码情操陶冶-AbstractApplicationContext#registerBeanPostProcessors 约定web.xml配置的contextClass为默认值X ...

  7. Spring源码情操陶冶-AbstractApplicationContext#registerBeanPostProcessors

    承接前文Spring源码情操陶冶-AbstractApplicationContext#invokeBeanFactoryPostProcessors 瞧瞧官方注释 /** * Instantiate ...

  8. Spring源码情操陶冶-AbstractApplicationContext#invokeBeanFactoryPostProcessors

    阅读源码有利于陶冶情操,承接前文Spring源码情操陶冶-AbstractApplicationContext#postProcessBeanFactory 约定:web.xml中配置的context ...

  9. Spring源码情操陶冶-AbstractApplicationContext#postProcessBeanFactory

    阅读源码有利于陶冶情操,承接前文Spring源码情操陶冶-AbstractApplicationContext#prepareBeanFactory 约定:web.xml中配置的contextClas ...

随机推荐

  1. SQL Server Alwayson读写分离配置

    标签:MSSQL/只读路由 概述 Alwayson相对于数据库镜像最大的优势就是可读副本,带来可读副本的同时还添加了一个新的功能就是配置只读路由实现读写分离:当然这里的读写分离稍微夸张了一点,只能称之 ...

  2. MVC 5限制所有HTTP请求必须是POST

    今天有位同事,提出了这样一个问题,他想限制所有MVC接收到的HTTP请求必须是POST方式. 接下来在下面的内容中,将我想到的方式分享给大家,如果大家有其它的方式,请留言. 一.HttpPostAtt ...

  3. eclipse下建立 android 项目,相关文件夹介绍

    今天开始进入ANDROID开发,之前一直做些JAVA的WEBSERVICE之类的文件,第一次从头开始整理ANDROID项目,我会把最近遇到的问题做一一梳理. 现在来说一下建立ANDROID项目后产生的 ...

  4. Nginx——在Windows环境下安装

    下载 Nginx是开源软件,用户可以访问 http://nginx.org/ 网站获取源码包或Windows二进制文件下载.其中1.13.x版本为开发版本,1.12.0版本为稳定版本.开发版本分支会较 ...

  5. js中的数组对象排序(方法sort()详细介绍)

    定义和用法 sort() 方法用于对数组的元素进行排序. 语法    arrayObject.sort(sortby) 参数sortby:可选.规定排序顺序.必须是函数. 返回值 对数组的引用.请注意 ...

  6. 正则表达式去除字符串左右空格函数 调用方法是,str.Trim();

    正则表达式去除字符串左右空格函数 调用方法是,str.Trim(); String.prototype.Trim = function() { return this.replace(/(^\s*)| ...

  7. php打包文件为ZIP包后下载到本地

    这是一个工作中需要打包下载当前产品的所有图片到本地,文件格式为ZIP压缩包,打包下载文件跟图片一样,本程序细节为实际情况,使用需按照自己实际情况书写:<?php/**************** ...

  8. 【LeetCode】141. Linked List Cycle

    题目: Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using ...

  9. 【Android Developers Training】 81. 解析XML数据

    注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...

  10. Example016实现下拉框

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...