Purpose of ContextLoaderListener in Spring
The ApplicationContext
is where your Spring beans live. The purpose of the ContextLoaderListener
is two-fold:
to tie the lifecycle of the
ApplicationContext
to the lifecycle of theServletContext
andto automate the creation of the
ApplicationContext
, so you don't have to write explicit code to do create it - it's a convenience function.
Another convenient thing about the ContextLoaderListener
is that it creates a WebApplicationContext
and WebApplicationContext
provides
access to the ServletContext
via ServletContextAware
beans and the getServletContext
method.
ServletContext定义了一些方法方便Servlet和Servlet容器进行通讯,在一个web应用中所有的Servlet都共用一个ServletContext,Spring在和web应用结合使用的时候,
是将Spring的容器存到ServletContext中的;而ServletContextListener用于监听ServletContext一些事件。
WebApplicationContext
. Simply delegates to ContextLoader
as well as to ContextCleanupListener
.public class ContextLoaderListener extends ContextLoader implements ServletContextListener {
/**
* Initialize the root web application context.
*/
@Override
public void contextInitialized(ServletContextEvent event) {
initWebApplicationContext(event.getServletContext());
} /**
* Close the root web application context.
*/
@Override
public void contextDestroyed(ServletContextEvent event) {
closeWebApplicationContext(event.getServletContext());
ContextCleanupListener.cleanupAttributes(event.getServletContext());
}
在initWebApplicationContext里面有详细信息
if (this.context == null) {
this.context = createWebApplicationContext(servletContext);
}
if (this.context instanceof ConfigurableWebApplicationContext) {
ConfigurableWebApplicationContext cwac = (ConfigurableWebApplicationContext) this.context;
if (!cwac.isActive()) {
// The context has not yet been refreshed -> provide services such as
// setting the parent context, setting the application context id, etc
if (cwac.getParent() == null) {
// The context instance was injected without an explicit parent ->
// determine parent for root web application context, if any.
ApplicationContext parent = loadParentContext(servletContext);
cwac.setParent(parent);
}
configureAndRefreshWebApplicationContext(cwac, servletContext);
}
}
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);
由最后一句可知,WeApplicationContext的实例,存放在servletContext中。

文件内容:
# Default WebApplicationContext implementation class for ContextLoader.
# Used as fallback when no explicit context implementation has been specified as context-param.
# Not meant to be customized by application developers. org.springframework.web.context.WebApplicationContext=org.springframework.web.context.support.XmlWebApplicationContext
Purpose of ContextLoaderListener in Spring的更多相关文章
- ContextLoaderListener和Spring MVC中的DispatcherServlet加载内容的区别
一:ContextLoaderListener加载内容 二:DispatcherServlt加载内容 ContextLoaderListener和DispatcherServlet都会在Web容器启动 ...
- [key]严重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener(Spring配置异常)
详细错误为: 严重: Exception sending context initialized event to listener instance of class org.springframe ...
- interface21 - web - ContextLoaderListener(Spring Web Application Context加载流程)
前言 最近打算花点时间好好看看spring的源码,然而现在Spring的源码经过迭代的版本太多了,比较庞大,看起来比较累,所以准备从最初的版本(interface21)开始入手,仅用于学习,理解其设计 ...
- ContextLoaderListener和Spring MVC中的DispatcherServlet学习
DispatcherServlet介绍 DispatcherServlet是Spring前端控制器的实现,提供Spring Web MVC的集中访问点,并且负责职责的分派,与Spring IoC容器无 ...
- ContextLoaderListener和Spring MVC中的DispatcherServlet学习 随手记
Servlet上下文关系 DispatcherServlet的上下文是通过配置servlet的contextConfigLocation来加载的,默认实现是XmlWebApplicationConte ...
- ContextLoaderListener和Spring MVC中的DispatcherServlet加载内容的区别【转】
原文地址:https://blog.csdn.net/py_xin/article/details/52052627 ContextLoaderListener和DispatcherServlet都会 ...
- Spring源码情操陶冶-ContextLoaderListener
前言:通过实例结合源码的方式解读,其中涉及到的文件来自于博主的Github毕设项目wxServer Note: Springboot应用不在本文章讨论范围 web.xml中启用Spring 在一般的w ...
- Spring MVC启动过程(1):ContextLoaderListener初始化
此文来自https://my.oschina.net/pkpk1234/blog/61971 (写的特别好)故引来借鉴 Spring MVC启动过程 以Tomcat为例,想在Web容器中使用Spirn ...
- Spring MVC源码分析(一):ContextLoaderListener的设计与实现
ContextLoaderListener在我的Spring源码分析(一):从哪里开始看spring源码这篇文章,分析过在web容器,如tomcat,启动web应用时,会通过监听器的方式,通知Serv ...
随机推荐
- linphone 调试信息
root@phyCORE-AM335x:~ linphonec -V -d 6INFO: no logfile, logging to stdoutortp-message-oRTP-0.20.0 i ...
- UCOS2_STM32移植详细过程(汇总)
Ⅰ.概述 笔者发现一个问题,很多初学者,甚至很多工作一两年的人,他们有一种依赖的思想,就是希望从别处获取的软件代码不做任何修改,直接可以运行或者使用.笔者想说,实践才是检验真理的关键,实践才是掌握知识 ...
- asp.net 简单分页打印
<html> <head> <title>看看</title> <meta http-equiv="Content-Type" ...
- 关于ajax跨域的一些说说
跨域:跨当然是跨过去,域当然是别的服务器 (说白点就是去别服务器上取东西) 只要协议.域名.端口有任何一个不同,都被当作是不同的域 ajax 是一种请求响应无刷新技术(xmlhttqrequest对象 ...
- [velocity] velocity详解
(1)为什么要使用模版语言? 在服务器端可以使用 Velocity 处理模板和生成的动态内容(HTML.XML等).这和 JSP 技术的目标非常接近.但是,JSP 模型可以毫无阻碍地访问底层的 Ser ...
- Hive三种不同的数据导出的方式
转自:http://blog.chinaunix.net/uid-27177626-id-4653808.html Hive三种不同的数据导出的方式,根据导出的地方不一样,将这些方法分为三类:(1)导 ...
- Servlet采用多线程来处理多个请求同时访问
Servlet采用多线程来处理多个请求同时访问,Servlet容器维护了一个线程池来服务请求.
- 【BZOJ】1637: [Usaco2007 Mar]Balanced Lineup(前缀和+差分+特殊的技巧)
http://www.lydsy.com/JudgeOnline/problem.php?id=1637 很神思想.. 前缀和应用到了极点... 我们可以发现当数量一定时,这个区间最前边的牛的前边一个 ...
- 隐马尔可夫树(HMT模型)
HMT(Hidden Markov Tree)隐马尔可夫树 [论文] 小波变换与HMT模型的图像插值算法-郭昌-中山大学学报(自然科学版)
- 打印出现:You've implemented -[<UIApplicationDelegate> application:didReceiveRemoteNotification:
解决办法: 第一种:Product -> Target -> Capabilities -> Background Modes -> 勾选Remote notification ...