Spring's MVC and Web Environment

    Spring MVC is built on top of the IOC container.

   DispachServlet and ContextLoaderListener provide interfaces to Spring in the Web container. The coupling between these interfaces and the Web container is realized through ServletContext. ServletContext provides a host environment for Spring's IOC container, and Spring MVC establishes an IOC container system. ContextLoaderListener is defined as a listener, which is responsible for completing the startup of the IOC container in the Web environment. DispatcherServlet plays the role of distributing requests and defines the corresponding URL mapping.

  <servlet>
    <servlet-name>SpringDispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>SpringDispatcher</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
 <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

The startup process of the IOC container

     The startup process of the IOC container is the process of establishing the context, which is accompanied by the ServletContext.

    Spring uses the default XmlWebApplicationContext as the IOC container.

ContextLoaderListener

    The function of ContextLoaderListener is to start the IOC container and load it into the WEB container. It is the first place for the entire sping web application to load the IOC container. It implements the ServletContextListener interface and provides callbacks (contextInitialized, contextDestroyed) combined with the servlet life cycle. The context created by ContextLoaderListener is the root context, and the parent context of the context created by DispatcherServlet is the root context. The specific process of loading the IOC container is completed by its base class ContextLoader.

ContextLoader completes the establishment of two IOC containers, one is to establish a parent IOC container in the Web container, and the other generates and initializes the corresponding WebApplicationContext.

DispatcherServlet

    DispatcherServlet will establish its own context to hold Spring MVC Bean objects. When establishing the IOC container it holds, it will get the root context from ServletContext as the parent context of the context held by DispatcherServlet. Finally, save your own context to the ServletContext.

    The work of DispacherServlet is mainly divided into two parts: one is the initialization part, which is started by initServletBean(), and finally calls the initStrategies() method of DispatcherServlet through initWebApplicationContext() to initialize other parts of the MVC module (such as internationalization, support for request mapping) HandlerMapping, etc.); the other is to respond to HTTP requests. As a servlet, the web container will call the servlet's doGet() and doPost() methods, and finally call the DispatcherServlet's doService() method, and then call its encapsulated doDispacher() method.

//MVC框架的初始化
protected void initStrategies(ApplicationContext context) {
    initMultipartResolver(context);
	initLocaleResolver(context);
	initThemeResolver(context);
	initHandlerMappings(context);
	initHandlerAdapters(context);
	initHandlerExceptionResolvers(context);
	initRequestToViewNameTranslator(context);
	initViewResolvers(context);
	initFlashMapManager(context);
}

MVC handles HTTP dispatch requests

In the doService method of the DispatcherServlet class, first set some corresponding attribute instances for the Request request, and then call the doDispatch method to distribute and process the request. Here is the actual process:

1. In the doDispatch method, call the checkMultipart method to determine whether the request is a Multipart request (such as file upload), and if so, use the MultipartResolver instance to convert the request to a Multipart request.

2. Call the getHandler method to get the HandlerExecutionChain instance object. In this method, first find the current HandlerMapping instance, and then call the HandlerMapping instance getHandler method to obtain the HandlerExecutionChain instance, which wraps the HandlerMapping instance object, and sets the matching interceptor in the instance.

3. Call the getHandlerAdapter method to find the corresponding HandlerAdapter adapter instance for the current HandlerMapping instance.

4. Call the applyPreHandler method of the HandlerExecutionChain instance to call the preHandler method (pre-intercept method) of its interceptor for execution. If it returns true, continue processing, otherwise stop the method call processing. (pre-intercept method execution call)

5. Call the handler method of the HandlerAdapter adapter instance to process the current request and return the corresponding ModelAndView object. ( Call the corresponding method of the target object to execute )

6. Call the applyPostHandler method of the HandlerExecutionChain instance to call the postHandler method (post-intercept method) of its interceptor for execution. ( Post-intercept method execution call )

7. Call the processDispatchResult method to process the final result (ModelAndView or exception), which calls the render method to render and parse the final view. In the render method, the View object of the corresponding view name is obtained through the ViewResolver parser, and finally the render method of the corresponding View object is called to render and process the view, and respond to the corresponding result to the client. ( Call the corresponding View to render the view )

Special beans used in DispatcherServlet

DispatcherServlet uses WebApplicationContext as the context by default, so let's take a look at what special beans are in this context:

1. Controller : processor/page controller, which does the things of C in MVC, but the control logic is transferred to the front-end controller for processing requests;

2. HandlerMapping : The mapping from requests to processors, if the mapping is successful, a HandlerExecutionChain object (including a Handler processor (page controller) object, multiple HandlerInterceptor interceptors) objects is returned; for example, BeanNameUrlHandlerMapping maps URLs to Bean names, and the mapping is successful The Bean is the processor here;

3. HandlerAdapter : HandlerAdapter will package the processor as an adapter, so as to support multiple types of processors, that is, the application of the adapter design pattern, so it is easy to support many types of processors; for example, SimpleControllerHandlerAdapter will implement the Bean that implements the Controller interface. Adapt, and drop the handleRequest method of the processor for functional processing;

4. ViewResolver : ViewResolver will parse the logical view name into a specific View. Through this strategy mode, it is easy to replace other view technologies; for example, InternalResourceViewResolver maps logical view names to jsp views;

5. LocalResover : Localization analysis, because Spring supports internationalization, so LocalResover parses the client's Locale information to facilitate internationalization;

6. ThemeResovler : Theme analysis, through which multiple styles of a page can be realized, that is, common effects similar to software skins;

7. MultipartResolver : file upload analysis, used to support file upload;

8. HandlerExceptionResolver : Handler exception parsing , which can map exceptions to the corresponding unified error interface, so as to display a user-friendly interface (instead of showing specific error messages to users);

9. RequestToViewNameTranslator : When the processor does not return the logical view name and other related information, the request URL is automatically mapped to the logical view name;

10. FlashMapManager : The strategy interface used to manage FlashMap , FlashMap is used to store the output of a request, and when entering another request, it is used as the input of the request, which is usually used for redirection scenarios, which will be described in detail later.

 

 

 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325445347&siteId=291194637