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一些事件。

  1.  
Bootstrap listener to start up and shut down Spring's root WebApplicationContext. Simply delegates to ContextLoader as well as to ContextCleanupListener.
 
1.分析
  1. public class ContextLoaderListener extends ContextLoader implements ServletContextListener {
  2. /**
  3. * Initialize the root web application context.
  4. */
  5. @Override
  6. public void contextInitialized(ServletContextEvent event) {
  7. initWebApplicationContext(event.getServletContext());
  8. }
  9.  
  10. /**
  11. * Close the root web application context.
  12. */
  13. @Override
  14. public void contextDestroyed(ServletContextEvent event) {
  15. closeWebApplicationContext(event.getServletContext());
  16. ContextCleanupListener.cleanupAttributes(event.getServletContext());
  17. }

在initWebApplicationContext里面有详细信息

  1. if (this.context == null) {
  2. this.context = createWebApplicationContext(servletContext);
  3. }
  4. if (this.context instanceof ConfigurableWebApplicationContext) {
  5. ConfigurableWebApplicationContext cwac = (ConfigurableWebApplicationContext) this.context;
  6. if (!cwac.isActive()) {
  7. // The context has not yet been refreshed -> provide services such as
  8. // setting the parent context, setting the application context id, etc
  9. if (cwac.getParent() == null) {
  10. // The context instance was injected without an explicit parent ->
  11. // determine parent for root web application context, if any.
  12. ApplicationContext parent = loadParentContext(servletContext);
  13. cwac.setParent(parent);
  14. }
  15. configureAndRefreshWebApplicationContext(cwac, servletContext);
  16. }
  17. }
          
  18. servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);

由最后一句可知,WeApplicationContext的实例,存放在servletContext中。

文件内容:

  1. # 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.
  2.  
  3. 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. LIB 配置文件读写器

    由于读写配置文件的次数比较频繁,而且拥有众多的类库,到最后,直接被各种各样的类库烦死. 顺手封一个简单,实用的.主要功能: 读写AppSetting 读取连接字符串 读取自定义配置节 using Sy ...

  2. BZOJ3776 : 警察局

    怎么3776又换题目了…换题目了…题目了…目了…了… SCC缩点后只有入度或者出度为0的点必须要放警察局 假设一共有t-1个入度或者出度为0的SCC q[1]-q[t-1]表示这些SCC中点的个数 q ...

  3. MONO 架构

  4. CCSpriteBatchNode的优化性能

    当将大量精灵加载到CCLayer时,如果直接利用[self addChild:sprite]去加载,每加载一个精灵,都必须open,draw,close, 而利用 CCSpriteBatchNode去 ...

  5. USACO 5.4 Telecowmunication(最大流+枚举)

    面对最小割之类的题目,完全木想法... 枚举+最大流..复杂度很大了...居然很快的就过了.. /* ID: cuizhe LANG: C++ TASK: telecow */ #include &l ...

  6. POJ 2486 Apple Tree(树形DP)

    题目链接 树形DP很弱啊,开始看题,觉得貌似挺简单的,然后发现貌似还可以往回走...然后就不知道怎么做了... 看看了题解http://www.cnblogs.com/wuyiqi/archive/2 ...

  7. 基于开源Dubbo分布式RPC服务框架的部署整合

    一.前言 Dubbo 作为SOA服务化治理方案的核心框架,用于提高业务逻辑的复用.整合.集中管理,具有极高的可靠性(HA)和伸缩性,被应用于阿里巴巴各成员站点,同时在包括JD.当当在内的众多互联网项目 ...

  8. Nginx在Windows系统和Linux系统下的重启

    一.Windows系统下重启nginx 1.杀掉nginx进程 tskill nginx echo 已终止所有ginx进程      2.启动nginx cd f:\nginx-  nginx.exe ...

  9. sftp配置

    sftp不需要安装,只需要借助sshd服务器即可使用. 增加用户useradd -s /bin/false mysftp 设置用户密码passwd mysftp 创建用户家目录mkdir /home/ ...

  10. C# 取整

    double a = 1.1478; Math.Celling(a): 向上取整,结果为2 Math.Float(a); 向下取整,结果为1 Math.Round(a,2);  保留两位小数的奇进偶舍 ...