The difference between the applicationContext loaded by Spring ContextLoaderListener and DispatcherServlet

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


Spring loads the context configuration file by configuring ContextLoaderListener in web.xml, and can also load the spring context configuration file in DispatcherServlet, then these two What's the difference.

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 a 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.


Supplement:

from: http://blog.csdn.net/wym1581/article/details/46467185

ContextLoaderListener initialized context: Bean, configuration file, context-loaded Bean for dao, service
DispatcherServlet initialization is only valid for spring Web MVC Bean, such as Controller, HandlerMapping, HandlerAdapter, etc., the initialization context should only load
the container webApplicationContext of the Web-related component DispatcherServlet, which inherits the ContextLoaderListener container, so that the beans to be used in the dispatcherservlet can be obtained from the webapplicationcontext.
Detailed dispatcherservlet:
The dispatcherServlet initialization process mainly does two things:
1. Initialize the web context used by spring mvc, and may specify the parent container (the root context of contextloaderlistener)
2. Initialize the strategy used by dispatcherservlet, such as handermapping handeradapter and other
dispatcherservlets Default configuration file: dispatcherservlet.properties, there are some special beans in this configuration file, these beans can be started without us registering, if we register these beans, the default beans will not register the special beans in
this configuration file List:
controller
handlermapping (request-to-handler mapping, if the mapping is successful, it returns a handlerexecutionchain object)
handleradapter (packages the handler as an adapter, which is the application of the adapter design pattern)
viewresolver (resolves the logical view name to a specific view, such as internalresourceviewresolver Map the logical view name to the jsp view)
localresolver (localization resolution, because spring supports internationalization, so localresolver parses the client's local information to facilitate internationalization)
themeresolver (theme resolution, through which to achieve multiple styles of a page)
multipartresolver (File upload analysis)
handlerexceptionresolver (handler exception resolution, which can map exceptions to the corresponding unified error interface)
requesttoviewnametranslator (when the processor does not have a logical view name, automatically maps the request url to a logical view name)
flashmapmanager (strategy interface for managing flashmap, flashmap Used to store the output of a request, when entering another request as the input of the request, usually used in redirection scenarios)

Guess you like

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