Spring ContextLoaderListener与DispatcherServlet所加载的applicationContext的区别

    xiaoxiao2021-03-25  90

    一:ContextLoaderListener加载内容   <context-param>     <param-name>contextConfigLocation</param-name>     <param-value>classpath*:config/spring-*.xml</param-value>   </context-param>   <listener>     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>   </listener> 二:DispatcherServlt加载内容   <servlet>     <servlet-name>springMVC</servlet-name>     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>     <init-param>       <param-name>contextConfigLocation</param-name>       <param-value>classpath*:config/spring-mvc.xml</param-value>     </init-param>      <load-on-startup>1</load-on-startup>   </servlet>   <servlet-mapping>     <servlet-name>springMVC</servlet-name>     <url-pattern>/</url-pattern>   </servlet-mapping> 区别    1、存放位置ContextLoaderListener中加载的context成功后,spring 将 applicationContext存放在ServletContext中key值为”org.springframework.web.context.WebApplicationContext.ROOT“的attribute中。可以通过WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext) 或WebApplicationContextUtils.getWebApplicationContext(servletContext) 方法来获取对应的applicationContext。 源码   servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, context);   DispatcherServlet加载的context成功后,如果 publishContext属性的值设置为true的话(缺省为true) 会将applicationContext存放在org.springframework.web.servlet.FrameworkServlet.CONTEXT. + (servletName)的attribute中。 可以通过 RequestContextUtils.getWebApplicationContext 或 WebApplicationContextUtils.getWebApplicationContext(servletContext,attrname)方法 来获取对应的applicationContext。 源码   if (this.publishContext) {   // Publish the context as a servlet context attribute.   String attrName = getServletContextAttributeName();   getServletContext().setAttribute(attrName, wac);   if (this.logger.isDebugEnabled()) {     this.logger.debug("Published WebApplicationContext of servlet '" + getServletName() +     "' as ServletContext attribute with name [" + attrName + "]");   }   }   2、DispatcherServlet所加载的applicationContext可以认为是mvc私有的context,由于保存在servletContext中的key值与通过ContextLoaderListener加载进来的applicationContext使用的key值不相同,因此如果只使用DispatcherServlet加载context的话,如果程序中有地方使用WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext) 来试图获取applicationContext时,就会抛出”No WebApplicationContext found: no ContextLoaderListener registered?”的exception,而通过WebApplicationContextUtils.getWebApplicationContext(servletContext)则取不到context,因此默认返回null.   3、ContextLoaderListener和DispatcherServlet都会在Web容器启动的时候加载一下bean配置,     DispatcherServlet一般会加载MVC相关的bean配置管理(如: ViewResolver, Controller, MultipartResolver, ExceptionHandler, etc.)     ContextLoaderListener一般会加载整个Spring容器相关的bean配置管理(如: Log, Service, Dao, PropertiesLoader, etc.)     DispatcherServlet默认使用WebApplicationContext作为上下文.     DispatcherServlet的上下文仅仅是Spring MVC的上下文, 而ContextLoaderListener的上下文则对整个Spring都有效. 一般Spring web项目中同时会使用这两种上下文.   4、WebApplicationContext将之前ContextLoaderListener创建的容器作为父容器,因此在父容器中配置的所有Bean都能够被注入到子容器中。 
    转载请注明原文地址: https://ju.6miu.com/read-12296.html

    最新回复(0)