一、ServletContext
addListener(..) 方法,也有创建的方法 createListener(Class<T> c)
addFilter(..) 方法,也有创建的方法。
可以获取路径,也可以获取其中的Servlet。可以获取资源,获取文件的MIME类型等等。
 
二、ServletContextListener
在ServletContext发生Event时,接收Event通知。
有两个方法:
// 在ServletContext进行初始化时执行。是在任何filter或者Servlet初始化之前。
public void contextInitialized(ServletContextEvent sce); // 在ServletContext销毁时执行。是在所有的filter和Servlet销毁之后进行。
public void contextDestroyed(ServletContextEvent sce);
 
三、org.springframework.web.context.ContextLoader
public class ContextLoader
文档描述如下:
  执行root application context的实际初始化工作。由ContextLoaderListener调用。
  会先去查找web.xml中context-param级别的contextClass参数,该参数指定了context class类型,如果找不到,会使用XmlWebApplicationContext。
  默认情况下,所有context class都需要实现ConfigurableWebApplicationContext。(除非自行写了一个新的ContextLoader,见下面的代码第三行)
     protected WebApplicationContext createWebApplicationContext(ServletContext sc) {
Class<?> contextClass = determineContextClass(sc);
if (!ConfigurableWebApplicationContext.class.isAssignableFrom(contextClass)) {
throw new ApplicationContextException("Custom context class [" + contextClass.getName() +
"] is not of type [" + ConfigurableWebApplicationContext.class.getName() + "]");
}
return (ConfigurableWebApplicationContext) BeanUtils.instantiateClass(contextClass);
}
  处理context-param的contextConfigLocation参数,传值给context实例,解析成多个文件路径(该参数可以使用逗号和空格设置多个值)。支持Ant风格。
  如果没指定,使用context class中默认的位置。例如,XmlWebApplicationContext使用 /WEB-INF/applicationContext.xml 。
 
  除了加载root application context之外,这个class还可以加载或获取并勾连到一个共享的父context。--就是给这个context设置一个父context。
 
 
四、现在再来看org.springframework.web.context.ContextLoaderListener
public class ContextLoaderListener extends ContextLoader implements ServletContextListener
文档描述如下:
  Bootstrap listener to start up and shut down Spring's root WebApplicationContext. Simply delegates to ContextLoader as well as to ContextCleanupListener. 
  引导监听器,用于启动和关闭Spring的根WebApplication上下文。
 
注意,该类除了两个构造方法之外,就只有重写了的ServletContextListener接口的两个方法。
public class ContextLoaderListener extends ContextLoader implements ServletContextListener {
public ContextLoaderListener() {
} public ContextLoaderListener(WebApplicationContext context) {
super(context);
} @Override
public void contextInitialized(ServletContextEvent event) {
initWebApplicationContext(event.getServletContext());
} @Override
public void contextDestroyed(ServletContextEvent event) {
closeWebApplicationContext(event.getServletContext());
ContextCleanupListener.cleanupAttributes(event.getServletContext());
} }
就是说,当ServletContext初始化时,会执行contextInitialized(..),然后 initWebApplicationContext(..)
  -- 这个方法【initWebApplicationContext(..)】先会去创建一个webApplicationContext,并设置父context(可能)。然后配置并刷新这个webApplicationContext,并将其以【WebApplicationContext.class.getName() + ".ROOT"】为key设为servletContext的属性。
 
 
那个带参构造是什么意思,什么情况需要通过webApplicationContext来构造?? -- 进入了一个思维盲区!该构造的目的是通过listener实例来注册listener(而非通过类名)。
带参构造的官方文档:
  使用给定的application context创建一个新的ContextLoaderListener。
  在Servlet 3.0+ 环境中有用。
  通过listener实例来注册listener(而非通过类名),见 javax.servlet.ServletContext.addListener 。
 
配合 initWebApplicationContext(..) 中的这个判断就明白了:
if (this.context == null) {
this.context = createWebApplicationContext(servletContext);
}
就是说,如果已有application context,就不再创建,直接使用已有的。但是该设置的一样设置。
 
总结:
ContextLoaderListener 这个类的作用是,通过监听创建并设置WebApplicationContext。嗯,记住,这是一个ServletContext监听器。
 
 

ContextLoader,ContextLoaderListener解读的更多相关文章

  1. Spring Framework 官方文档学习(二)之IoC容器与bean lifecycle

    到目前为止,已经看了一百页.再次感慨下,如果想使用Spring,那可以看视频或者找例子,但如果想深入理解Spring,最好还是看官方文档. 原计划是把一些基本接口的功能.层次以及彼此的关系罗列一下.同 ...

  2. Spring的监听器ContextLoaderListener

    一.作用 ContextLoaderListener监听器的作用就是启动web容器时,自动装配ApplicationContext的配置信息.它实现了ServletContextListener接口, ...

  3. Spring MVC源码——Root WebApplicationContext

    目录 Spring MVC源码--Root WebApplicationContext 上下文层次结构 Root WebApplicationContext 初始化和销毁 ContextLoaderL ...

  4. Spring MVC 解读——<context:component-scan/>

    转自:http://my.oschina.net/HeliosFly/blog/203149 作者:GoodLoser. Spring MVC 解读---<context:component-s ...

  5. Spring源码情操陶冶-ContextLoader

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

  6. Spring源码情操陶冶-ContextLoaderListener

    前言:通过实例结合源码的方式解读,其中涉及到的文件来自于博主的Github毕设项目wxServer Note: Springboot应用不在本文章讨论范围 web.xml中启用Spring 在一般的w ...

  7. SpringMVC 原理 - 设计原理、启动过程、请求处理详细解读

    SpringMVC 原理 - 设计原理.启动过程.请求处理详细解读 目录 一. 设计原理 二. 启动过程 三. 请求处理 一. 设计原理 Servlet 规范 SpringMVC 是基于 Servle ...

  8. [Spring框架] Spring中的 ContextLoaderListener 实现原理.

    前言: 这是关于Spring的第三篇文章, 打算后续还会写入AOP 和Spring 事务管理相关的文章, 这么好的两个周末 都在看code了, 确实是有所收获, 现在就来记录一下. 在上一篇讲解Spr ...

  9. contextloaderlistener

    http://blog.csdn.net/c5153000/article/details/6234207 作用:在启动Web容器时,自动装配Spring applicationContext.xml ...

随机推荐

  1. 在Flex (Flash)中嵌入HTML 代码或页面—Flex IFrame

    在flex组件中嵌入html代码,可以利用flex iframe.这个在很多时候会用到的,有时候flex必须得这样做,如果你不这样做还真不行…… flex而且可以和html进行JavaScript交互 ...

  2. Android相机基础基于camera2API

    前言 最近,在使用Android做一个照相机的开发.因为不能使用系统提供的相机应用,所以只能自己写一个.Android以前提供相机的api叫camera,不过在level 21被Google抛弃了.网 ...

  3. 算法笔记_151:算法提高 01背包(Java)

    目录 1 问题描述 2 解决方案   1 问题描述 问题描述 给定N个物品,每个物品有一个重量W和一个价值V.你有一个能装M重量的背包.问怎么装使得所装价值最大.每个物品只有一个. 输入格式 输入的第 ...

  4. studio快捷键

  5. OpenERP函數字段的應用

    在ERP開發過程中經常會使用到某字段的值是由其他字段計算得來,並且有些還需要將計算的結果存入資料庫. 以上功能上OpenERP中是用field.function實現的 其中有種模式 a). 只計算,不 ...

  6. (一)《Spring实战》——Spring核心

    <Spring实战>(第4版) 第一章:Spring之旅 1. 简化Java开发 为了降低Java开发的复杂性,Spring采取了以下4种关键策略: 基于POJO的轻量级和最小侵入性编程: ...

  7. python基础之Event对象、队列和多进程基础

    Event对象 用于线程间通信,即程序中的其一个线程需要通过判断某个线程的状态来确定自己下一步的操作,就用到了event对象 event对象默认为假(Flase),即遇到event对象在等待就阻塞线程 ...

  8. 机器学习基石第三讲:types of learning

    博客已经迁移至Marcovaldo's blog (http://marcovaldong.github.io/) 刚刚完毕机器学习基石的第三讲.这一讲主要介绍了机器学习的分类.对何种问题应该使用何种 ...

  9. C# Debug

    语法.IDE环境使用.Debug方法是学习一门语言的最少必须技能,本文总结C#中的最常用调试方法 一. 断点 如下图所示在欲插入断点的地方右键>断点>插入断点(或在行号左边点击)可在选中语 ...

  10. struts2 s:set标签

    set标签是将某个值放到指定范围内, 比如说 student.teacher.parent.age 每次访问这个属性不仅性能低,而且代码可读性很差,为了解决这个问题,可以将这个值设置为一个新值,并且放 ...