The ApplicationContext is where your Spring beans live. The purpose of the ContextLoaderListener is two-fold:

  1. to tie the lifecycle of the ApplicationContext to the lifecycle of the ServletContext and

  2. to 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 ServletContextvia ServletContextAware beans and the getServletContext method.

ServletContext定义了一些方法方便Servlet和Servlet容器进行通讯,在一个web应用中所有的Servlet都共用一个ServletContext,Spring在和web应用结合使用的时候,

是将Spring的容器存到ServletContext中的;而ServletContextListener用于监听ServletContext一些事件。

 
Bootstrap listener to start up and shut down Spring's root WebApplicationContext. Simply delegates to ContextLoader as well as to ContextCleanupListener.
 
1.分析
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
 
 

RequestContextListener有什么用的更多相关文章

  1. RequestContextListener作用

    spring IOC容器实例化Bean的方式有: singleton            在spring IOC容器中仅存在一个Bean实例,Bean以单实例的方式存在. prototype     ...

  2. spring IOC容器实例化Bean的方式与RequestContextListener应用

    spring IOC容器实例化Bean的方式有: singleton 在spring IOC容器中仅存在一个Bean实例,Bean以单实例的方式存在. prototype 每次从容器中调用Bean时, ...

  3. request.RequestContextListener

    由于是使用spring mvc来做项目,因此脱离了HttpServletRequest作为参数,不能够直接使用request,要想使用request可以使用下面的方法: 在web点xml中配置一个监听 ...

  4. Spring的作用域以及RequestContextListener作用<转>

    一.配置方式 在Spring2.0中除了以前的Singleton和Prototype外又加入了三个新的web作用域,分别为request.session和global session,如果你想让你的容 ...

  5. ContextLoaderListener与RequestContextListener配置问题

    转自:https://blog.csdn.net/yyqhwr/article/details/83381447 SSH2.SSM等web应用开发框架的配置过程中,因为都要用到spring,所以,往往 ...

  6. SSM框架中,ContextLoaderListener与RequestContextListener的联系与区别

    在整合SSM框架时,我们要在web.xml文件中对spring进行相关配置. 首先要配置一个<context-param></context-param> <!--加载s ...

  7. 在springmvc中使用requestContextListener获取全部的request对象

    RequestContextListener实现了 ServletRequestListener ,在其覆盖的requestInitialized(ServletRequestEvent reques ...

  8. spring笔记6 spring IOC的中级知识

    1,spring ioc的整体流程,xml配置 spring ioc初始化的流程结合上图 步骤编号 完成的工作 1 spring容器读取配置文件,解析称注册表 2 根据注册表,找到相应的bean实现类 ...

  9. Java web.xml 配置详解

    在项目中总会遇到一些关于加载的优先级问题,近期也同样遇到过类似的,所以自己查找资料总结了下,下面有些是转载其他人的,毕竟人家写的不错,自己也就不重复造轮子了,只是略加点了自己的修饰. 首先可以肯定的是 ...

随机推荐

  1. 简单几何(极角排序) POJ 2007 Scrambled Polygon

    题目传送门 题意:裸的对原点的极角排序,凸包貌似不行. /************************************************ * Author :Running_Time ...

  2. LCIS POJ 2172 Greatest Common Increasing Subsequence

    题目传送门 题意:LCIS(Longest Common Increasing Subsequence) 最长公共上升子序列 分析:a[i] != b[j]: dp[i][j] = dp[i-1][j ...

  3. BZOJ3680 : 吊打XXX

    本题就是找一个受力平衡的点 我们一开始假设这个点是(0,0) 然后求出它受到的力,将合力正交分解后朝着合力的方向走若干步,并不断缩小步长,一步步逼近答案 #include<cstdio> ...

  4. HDU 1312 (BFS搜索模板题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1312 题目大意:问迷宫中有多少个点被访问. 解题思路: DFS肯定能水过去的.这里就拍了一下BFS. ...

  5. FFMPEG解码流程

    FFMPEG解码流程:  1. 注册所有容器格式和CODEC: av_register_all()  2. 打开文件: av_open_input_file()  3. 从文件中提取流信息: av_f ...

  6. 清除 WD MyCloud 自动生成的 .wdmc 目录

    1. 先 SSH,停止相应服务 /etc/init.d/wdmcserverd stop/etc/init.d/wdphotodbmergerd stop 2. 禁止服务自启动 update-rc.d ...

  7. Unix Shell中单引号、双引号字符、反斜杠、反引号的使用[转]

    在执行shell脚本的时候,shell将会对脚本中的行进行解释,然后执行:对于一些特殊处理的句子,我们可以使用引号或者反斜线来避免shell解释执行之.如下,当在命令行中输入:echo *child. ...

  8. nova

    chen@controller:~$ nova usage: nova [--version] [--debug] [--os-cache] [--timings]             [--ti ...

  9. ionic 写一个五星评价(非指令)

    Controller里的代码: .controller('evaluateCtrl', function($scope, $state, $stateParams, $ionicPopup,$ioni ...

  10. 【转】bShare分享插件的使用

    原文地址:http://blog.csdn.net/pan_junbiao/article/details/17884203 1.引用JS文件 分享标签: 1.class="bshare-c ...