spring通过在web.xml 中配置ContextLoaderListener 来加载co...
2013-06-15 00:00
971 查看
摘要: spring通过在web.xml 中配置ContextLoaderListener 来加载context配置文件,在DispatcherServlet中也可以来加载spring context配置文件,那么这两个有什么区别呢。
可以重复配置
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中配置如下
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。
可以重复配置
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中配置如下
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。
相关文章推荐
- 搭建spring框架的时候,web.xml中的spring相关配置,可以不用配置ContextLoaderListener(即只配DispatcherServlet)吗?
- [Spring]web.xml中配置ContextLoaderListener监听器的作用
- web.xml中spring如何通过ContextLoaderListener初始化的
- web.xml配置详解 org.springframework.web.context.ContextLoaderListener
- Spring配置web.xml中<listener>标签(ContextLoaderListener类解析)解析
- web.xml文件中配置spring的ContextLoaderListener后报错
- 搭建spring框架的时候,web.xml中的spring相关配置,可以不用配置ContextLoaderListener(即只配DispatcherServlet)吗?
- 160329(一)、在web.xml文件里配置org.springframework.web.context.ContextLoaderListener
- [Spring]web.xml中配置ContextLoaderListener监听器的作用
- Spring源码学习之一 ContextLoaderListener及XmlWebApplicationContext
- 在搭建Spring框架时,web.xml文件的配置之DispatcherServlet&ContextLoaderListener
- SpringMVC源码 1 ContextLoaderListener和Servlet容器web.xml配置
- 原博文补充: web.xml配置中ContextLoaderListener和DispatcherServlet的区别: - ContextLoaderListener是在我们的web容器启动的时候启
- 通过web.xml文件自动启动spring容器加载对应的配置文件
- web.xml 通过contextConfigLocation配置spring 的方式
- eclipse中创建maven项目, maven无法加载org.springframework.web.context.ContextLoaderListener
- web.xml 文件的listener,filter,servlet的加载初始化顺序及ContextLoaderListener的小例子
- spring_JUnit4测试_通过注解加载xml配置_@RunWith_@ContextConfiguration
- 【web.xml】报错java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
- web.xml 通过contextConfigLocation配置spring 的方式(转)