一、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,见下面的代码第三行)
1     protected WebApplicationContext createWebApplicationContext(ServletContext sc) {
2         Class<?> contextClass = determineContextClass(sc);
3         if (!ConfigurableWebApplicationContext.class.isAssignableFrom(contextClass)) {
4             throw new ApplicationContextException("Custom context class [" + contextClass.getName() +
5                     "] is not of type [" + ConfigurableWebApplicationContext.class.getName() + "]");
6         }
7         return (ConfigurableWebApplicationContext) BeanUtils.instantiateClass(contextClass);
8     }

处理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);}@Overridepublic void contextInitialized(ServletContextEvent event) {initWebApplicationContext(event.getServletContext());}@Overridepublic 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监听器。

转载于:https://www.cnblogs.com/larryzeal/p/5885739.html

ContextLoader,ContextLoaderListener解读相关推荐

  1. springmvc学习记录

    <<看透springMvc源代码分析与实践.pdf>> spring Web MVC 框架提供"模型-视图-控制器"( Model-View-Control ...

  2. Spring MVC 解读——context:component-scan/

    2019独角兽企业重金招聘Python工程师标准>>> Spring MVC 解读---<context:component-scan/> 注解是骑士魂牵梦绕的美丽公主, ...

  3. SpringMVC容器初始化篇----ContextLoaderListener

    学习学习容器初始化,若有不对的地方,请指出更正,大家共同学习学习. 此篇幅主要围绕着 ContextLoaderListener加载容器,理解其中的原理. ContextLoaderListener的 ...

  4. 【Spring】23、ApplicationContext ,ApplicationContextAware,Listener,Event 的关系解读

    tomcat容器启动流程 启动tomcat容器,加载web.xml,建立整个容器(Servlet容器,这里是tomcat吧)的上下文,ServletContext,这时web.xml有个监听器,就是C ...

  5. ContextLoaderListener作用详解

    原文:http://blog.csdn.net/ysughw/article/details/8992322 ContextLoaderListener监听器的作用就是启动Web容器时,自动装配App ...

  6. Spring mvc ContextLoaderListener 原理解析

    对于熟悉Spring MVC功能,首先应从web.xml 开始,在web.xml 文件中我们需要配置一个监听器 ContextLoaderListener,如下. <!-- 加载spring上下 ...

  7. ContextLoaderListener的用途以及配置方式

    ContextLoaderListener的作用就是启动Web容器时,自动装配ApplicationContext的配置信息.因为它实现了ServletContextListener这个接口,在web ...

  8. web.xml中的ContextLoaderListener和DispatcherServlet区别

    web.xml中的ContextLoaderListener和DispatcherServlet区别 ContextLoaderListener和DispatcherServlet都会在Web容器启动 ...

  9. springmvc dao怎么可以不写实现类_SpringMVC(一)细聊ContextLoaderListener 是怎么被加载的...

    点击上方"蓝字"关注我们,每天点亮一个技能点. 本文作者:burgxun 云析学院VIP学员 前言 为什么要写博客呢?其实还是源于我在知乎上面的 看到的一个回答.问题是这样的:生活 ...

最新文章

  1. 什么是图神经网络 (GNN)?
  2. idea不区分大小写设置_我的 IntelliJ IDEA 一直都是这么设置的,效果很棒!
  3. linux ubuntu 开机自动启动 fixfox 并打开指定网站
  4. 关于make *.img时没有权限的问题
  5. python dict sorted
  6. POJ 1222 EXTENDED LIGHTS OUT(高斯消元)
  7. JDK 14的新特性:instanceof模式匹配
  8. CentOS 7 中firewall-cmd命令
  9. AI超人赛车手狂虐人类登Nature封面!1000台PS4训练,「苏菲」极限超车独霸赛道...
  10. 计算机模拟数学实验动画,计算机图形学实验-简单动画的实现、三维图形变换.docx...
  11. Ubuntu下Apache反向代理设置
  12. python中urllib.parse啥意思_python-urllib.parse模块简述
  13. C语言基础教程之强制类型转换
  14. JavaScript函数和对象
  15. 全概率公式和贝叶斯公式的定义与说明
  16. mariadb 的安装及基本配置
  17. 需求分析及技术方案设计
  18. 如何快速打开控制面板?如何让控制面板在桌面显示?
  19. 知识 | 四种渲染到底是啥?终于有人讲明白了(下)
  20. Suit and Tie (在线swap 贪心 思维)

热门文章

  1. Android中软键盘弹出时关于布局的问题
  2. hdu 1512 Monkey King 左偏树
  3. 嵌入式根文件系统的移植和制作详解
  4. KindleConverter:Word批量转换为6寸PDF
  5. 操作word打印网址
  6. Flutter 项目开发指导 从基础入门到精通使用目录
  7. 如何通过一个结构体成员变量的地址找到该结构体的首地址?[备忘]
  8. react-redux笔记
  9. Jenkins系列-Jenkins插件备份
  10. Vue和后台交互的方式