Spring ContextLoaderListener与DispatcherServlet所加载的applicationContext的区别
来源:互联网 发布:shell脚本编程教程 编辑:程序博客网 时间:2024/05/17 15:58
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中。
在每次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。
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。
阅读全文
0 0
- Spring ContextLoaderListener与DispatcherServlet所加载的applicationContext的区别
- Spring ContextLoaderListener与DispatcherServlet所加载的applicationContext的区别
- Spring ContextLoaderListener与DispatcherServlet所加载的applicationContext的区别
- Spring ContextLoaderListener与DispatcherServlet所加载的applicationContext的区别
- Spring ContextLoaderListener与DispatcherServlet所加载的applicationContext的区别
- Spring ContextLoaderListener与DispatcherServlet所加载的applicationContext的区别
- ContextLoaderListener与DispatcherServlet所加载的applicationContext的区别
- ContextLoaderListener与DispatcherServlet所加载的applicationContext的区别
- ContextLoaderListener与DispatcherServlet所加载的applicationContext的区别
- [j2ee][spring]ContextLoaderListener与DispatcherServlet所加载的applicationContext的区别
- Spring ContextLoaderListener 与 SpringMVC DispatcherServlet所加载的applicationContext的区别
- ContextLoaderListener与DispatcherServlet所加载的区别
- ContextLoaderListener和Spring MVC中的DispatcherServlet加载内容的区别
- spring中ContextLoaderListener和DispatcherServlet区别 ApplicationContext
- Spring中DispatcherServlet和ContextLoaderListener的区别
- Spring中ContextLoaderListener和DispatcherServlet的区别
- Spring 中ContextLoaderListener和DispatcherServlet所加载的context的关系
- DispatcherServlet与ContextLoaderListener的对比
- SpringBoot快速入门,简单样例
- 获取ContentProvider来获取手机所有的联系人及联系人信息
- gradle linux 绝对路径
- android UiAutomator长按实现控制按住控件时间的方法
- HDU 3714 Error Curves(三分)
- Spring ContextLoaderListener与DispatcherServlet所加载的applicationContext的区别
- php-china.org 的开源项目安装
- 《利用Python进行数据分析》学习笔记ch03(4)
- C++实现双链表基本接口
- OpenAirInterface USRP安装时You do not have write permissions at the install location问题解决方案
- 通过JPA来自动创建Model类与数据中表对应
- hdu 4506 快速幂
- cookie和session解析
- 第18章、java I/O系统