spring通过在web.xml 中配置ContextLoaderListener 来加载context配置文件,在DispatcherServlet中也可以来加载spring context配置文件,那么这两个有什么区别呢。

ContextLoaderListener中加载的context成功后,spring 将 applicationContext存放在ServletContext中key值为"org.springframework.web.context.WebApplicationContext.ROOT"的attribute中。(servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context));可以通过WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext)或WebApplicationContextUtils.getWebApplicationContext(servletContext)方法来获取对应的applicationContext。

DispatcherServlet加载的context成功后,如果 publishContext属性的值设置为true的话(缺省为true) 会将applicationContext存放在org.springframework.web.servlet.FrameworkServlet.CONTEXT. + (servletName)的attribute中。

例如 web.xml中配置如下

<servlet>  <servlet-name>mvcServlet</servlet-name>  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  <init-param>  <param-name>contextConfigLocation</param-name>  <param-value>classpath*:/spring/config/applicationContextMVC.xml</param-value>  </init-param>  <load-on-startup>1</load-on-startup>
</servlet> 

则对应的applicationContext的attribute key值为org.springframework.web.servlet.FrameworkServlet.CONTEXT.mvcServlet。

在每次request请求时,DispatcherServlet会将此applicationContext存放在request中attribute值为 org.springframework.web.servlet.DispatcherServlet.CONTEXT中(request.setAttribute(WEB_APPLICATION_CONTEXT_ATTRIBUTE,getWebApplicationContext());)。可以通过 RequestContextUtils.getWebApplicationContext 或 WebApplicationContextUtils.getWebApplicationContext(servletContext,attrname)方法 来获取对应的applicationContext。

从上面的分析可以看出,DispatcherServlet所加载的applicationContext可以认为是mvc私有的context,由于保存在servletContext中的key值与通过ContextLoaderListener加载进来的applicationContext使用的key值不相同,因此如果只使用DispatcherServlet加载context的话,如果程序中有地方使用WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext) 来试图获取applicationContext时,就会抛出"No WebApplicationContext found: no ContextLoaderListener registered?"的exception。

from: http://blog.csdn.net/madun/article/details/8988860

转载于:https://www.cnblogs.com/liuqingsha3/p/4218750.html

Spring ContextLoaderListener与DispatcherServlet所加载的applicationContext的区别相关推荐

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

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

  2. Spring Ioc 之 Bean的加载(3):createBean()

    createBean()是创建Bean的关键方法 一.createBean() 代码: //AbstractAutowireCapableBeanFactory.java protected Obje ...

  3. Spring Ioc 之 Bean的加载(1)(生命周期)

    在之前的文章中,我们分析了Spring的Ioc的初始化过程,实际上就是把 beanName 和 BeanDefinition 注册到DefaultListableBeanFactory的map中. 在 ...

  4. Spring 3.1 –从数据库加载XML配置的属性

    Spring使通过其PropertyPlaceholderConfigurer和(Spring 3.1之前)PropertySourcesPlaceholderConfigurer(Spring 3. ...

  5. Spring MVC中静态资源加载

    问题:MVC 静态资源加载,包括 js.css.image加载不出来 由于web.xml中dispatcherServlet中 url-pattern 使用 / 拦截所有访问,而导致静态资源也交给了D ...

  6. spring源码之bean加载(bean解析下篇)

    bean的加载步骤: MyTestBean bean = (MyTestBean) bf.getBean("myTestBean"); 步骤: (1) 转换对应的beanName ...

  7. Spring源码分析4---IOC加载过程补充总结

    原文出自:http://cmsblogs.com IOC 之 获取验证模型 DTD 与 XSD 的区别 DTD(Document Type Definition),即文档类型定义,为 XML 文件的验 ...

  8. Nacos Spring Cloud 实现配置热加载

    0x01:安装 Nacos 下载安装包, https://github.com/alibaba/nacos/releases 从下载 nacos-server-$version.zip 安装包. 解压 ...

  9. Spring源码——bean的加载

    前言 内容主要参考自<Spring源码深度解析>一书,算是读书笔记或是原书的补充.进入正文后可能会引来各种不适,毕竟阅读源码是件极其痛苦的事情. 本文主要涉及书中第五章的部分,依照书中内容 ...

最新文章

  1. python unix时间戳格式化输出_python正常时间和unix时间戳相互转换的方法
  2. Spring Cloud构建微服务架构:消息驱动的微服务(消费分区)【Dalston版】
  3. 单例模式应用场景_三、单例模式详解
  4. 如何将JAR添加到Jetbrains MPS项目
  5. Java基础——Java多线程中sleep()、wait()和notify()
  6. 使用XLog、Spring-Boot、And-Design-Pro搭建日志系统
  7. [VNC] 云服务器 Ubuntu 配置 VNC 遇到的问题
  8. [转载] 机器学习模型的保存和调用
  9. Log4j 日志输出学习(Eclipse)
  10. [网络安全自学篇] 八十四.《Windows黑客编程技术详解》之VS环境配置、基础知识及DLL延迟加载详解(1)
  11. linux支持labview2017,LabVIEW 2017
  12. 扫描仪 无线 打印服务器,自带扫描仪、还能无线打印,Find X2 Pro实用功能分享...
  13. 笔记本电脑上的以太网消失或者无法识别问题
  14. php 织梦手册,Dede标签快速手册
  15. Linux项目实战——五子棋(单机人人对战版)
  16. 阶乘末尾 0 的个数.c
  17. compact php,php compact()函数的使用方法详解
  18. 怎么申请沙特SABER认证及流程如何
  19. Substance的置换效果
  20. 【方案分享】阿里城市大脑数据智能解决方案.pdf(附下载链接)

热门文章

  1. tcpdump + wireshark 抓包组合
  2. 3、基于多播、安全认证的corosync集群(VIP、Httpd、Filesystem)
  3. windows禁用/启用hyper-V,解决hyper-V与模拟器同时启用时造成冲突
  4. Web安全测试检查单
  5. ThinkPHP实现定时执行任务的两种方法 - 博客频道 - CSDN.NET
  6. 【Python3_进阶系列_006】Python3-单例模式
  7. shell基础:环境变量
  8. 新手谈谈网站交互设计
  9. JPA基础(一):全面阐释和精彩总结JPA
  10. WinRar 代替之选:7-Zip+7zSfxTool