`

ContextLoaderListener与DispatcherServlet所加载的applicationContext的区别

 
阅读更多

 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中。

 

	<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。

分享到:
评论

相关推荐

    DispatcherServlet 和 ContextLoaderListener 区别

    NULL 博文链接:https://angie.iteye.com/blog/2334955

    Spring的监听器ContextLoaderListener的作用

    Spring的监听器ContextLoaderListener的作用

    web.xml中ContextLoaderListener的运行过程解析

    web.xml中ContextLoaderListener的运行

    java解决org.springframework.web.context.ContextLoaderListener

    java解决org.springframework.web.context.ContextLoaderListener

    ssh整合时遇到常见错误 ContextLoaderListener not found 解决

    ssh整合时 被虐的经验之谈。内容虽然比较少,也是前人的工作经验。

    基于java的企业级应用开发:Spring的核心容器.ppt

    spring/applicationContext.xml &lt;/param-value&gt; &lt;/context-param&gt; &lt;listener&gt; &lt;listener-class&gt; org.springframework.web.context.ContextLoaderListener &lt;/listener-class&gt; &lt;/listener&gt; ApplicationContext * * * *...

    Web项目中使用Spring, 使用 Spring 的器监听器 ContextLoaderListener.docx

    二、 使用 Spring 的器监听器 ContextLoaderListener o1. maven依赖pom.xml o2. 注册监听器 ContextLoaderListener o3. 指定 Spring 配置文件的位置 o4. 获取Spring容器对象 在 Web 项目中使用 Spring 框架,首先...

    ssm

    &lt;!-- 前端控制器:...-- 启动WEB容器时,自动装配ApplicationContext配置信息:ContextLoaderListener --&gt; &lt;!-- 确保web应用程序的类加载器以及加载的类正确释放资源:IntrospectorCleanupListener --&gt;

    企业人力资源管理项目SSH+EXT+MySQL+MD5

    name标签与welcome-file-list标签中加入一下代码 struts2 org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter struts2 /* org.springframework.web.context.ContextLoaderListener ...

    Java中的Listener监听器

    本文介绍了Listener以下几个方面的内容: ...  Spring使用ContextLoaderListener加载ApplicationContext配置信息  Spring使用Log4jConfigListener配置Log4j日志  Spring使用IntrospectorCleanupListener清理

    spring源代码解析

    下面我们使用ContextLoaderListener作为载入器作一个详细的分析,这个Servlet的监听器是根上下文被载入的地方,也是整个 Spring web应用加载上下文的第一个地方;从加载过程我们可以看到,首先从Servlet事件中得到...

    基于SSH模拟当当网项目(电子商务平台)

    可以利用Hibernate一对多映射,加载当前类别的子元素.(参考hibernate05工程Category示例) 2.分类浏览页面,左侧类别及其产品数量显示.数量统计可以采用Hibernate中formula映射实现. 3.购物车业务组件利用Spring中scope...

    gradle-spring-4-mvc-boilerplate

    1个通过侦听器加载的应用程序上下文几乎就像您已经完成的一样(如果调用文件applicationContext.xml并将其放在/ WEB-INF中,则甚至不需要使用上下文参数来指定其位置):代码: &lt;listener&gt;org.springframework....

    简单spring MVC 配置

    &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener &lt;servlet-name&gt;test &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet &lt;load-on-startup&gt;1 &lt;servlet-name&gt;...

    Spring MVC 入门实例

    上面, 我们在 web.xml 文件中告诉 ContextLoaderListener, 我们还有另外两个配置文件 /WEB-INF/database.xml 和 /WEB-INF/applicationContext.xml. applicationContext.xml: 1 2 &lt;!DOCTYPE beans PUBLIC "-//...

    springweb3.0MVC注解(附实例)

    &lt;param-value&gt;classpath:applicationContext.xml &lt;!-- Spring 容器启动监听器 --&gt; org.springframework.web.context.ContextLoaderListener &lt;/listener&gt; &lt;!-- Spring MVC 的Servlet,它将加载WEB-...

    Spring MVC 框架应用实例

    /WEB-INF/database.xml /WEB-INF/applicationContext.xml org.springframework.web.context.ContextLoaderListener &lt;filter-name&gt;encodingFilter org.springframework.web.filter....

    Proxool-0.9.1

    3.解决与spring配合时,只能在spring的定义中以bean方式加载proxool的问题。(使用servlet方式加载也可以,只是在启动时报错) 加载方式修改为: &lt;!-- proxool --&gt; &lt;param-name&gt;xmlFile &lt;param-value&gt;/WEB-...

    ssh框架的搭建

    /WEB-INF/applicationContext-*.xml,/WEB-INF/action-servlet.xml &lt;param-name&gt;log4jConfigLocation &lt;param-value&gt;/WEB-INF/log4j.properties &lt;!-- Listener contextConfigLocation --&gt; ...

    整合struts2和spring源代码(可以直接在tomcat中运行)

    Struts2与Spring的集成要用到Spring插件包struts2-spring-plugin-x-x-x.jar,这个包是同Struts2一起发布的。Spring插件是通过覆盖(override)Struts2的ObjectFactory来增强核心框架对象的创建。当创建一个对象的...

Global site tag (gtag.js) - Google Analytics