The difference between the applicationContext loaded by ContextLoaderListener and DispatcherServlet

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

You can also refer to my summary: http://angie.iteye.com/blog/2334955

Spring configures ContextLoaderListener in web.xml to To load the context configuration file, you can also load the spring context configuration file in DispatcherServlet, so what is the difference between the two.

After the context loaded in the ContextLoaderListener is successful, spring stores the applicationContext in the attribute whose key value is "org.springframework.web.context.WebApplicationContext.ROOT" in the ServletContext. (servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context)); You can obtain the corresponding applicationContext through the WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext) or WebApplicationContextUtils.getWebApplicationContext(servletContext) method.

After the context loaded by the DispatcherServlet is successful, if the value of the publishContext attribute is set to true (the default is true), the applicationContext will be stored in the attribute of org.springframework.web.servlet.FrameworkServlet.CONTEXT. + (servletName).

For example, the following

Xml code is configured in 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> 
The attribute key value of the corresponding applicationContext is org.springframework.web.servlet.FrameworkServlet.CONTEXT.mvcServlet.

  In each request request, DispatcherServlet will store this applicationContext in the request attribute value org.springframework.web.servlet.DispatcherServlet.CONTEXT (request.setAttribute(WEB_APPLICATION_CONTEXT_ATTRIBUTE, getWebApplicationContext());). The corresponding applicationContext can be obtained through RequestContextUtils.getWebApplicationContext or WebApplicationContextUtils.getWebApplicationContext(servletContext,attrname).

  It can be seen from the above analysis that the applicationContext loaded by DispatcherServlet can be considered as the private context of mvc. Since the key value stored in the servletContext is different from the key value used by the applicationContext loaded through ContextLoaderListener, if only DispatcherServlet is used to load the context If there is a place in the program to use WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext) to try to get the applicationContext, the exception of "No WebApplicationContext found: no ContextLoaderListener registered?" will be thrown.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326992216&siteId=291194637