[SpringMVC source code analysis] SpringMVC workflow

The SpringMVC workflow is shown in the figure:

You can see that DispatcherServlet is a core class that acts as a distributor. Its inheritance relationship is as follows:

It can be seen that DispatcherServlet inherits from Framework, inherits from HttpServletBean, and inherits from HttpServlet.

So DispatcherServler is a Servlet. It can be invoked and executed by the Servlet container.

 

Explain the flow chart with source code:

1. The user's request will be sent to DispatchServlet by tomcat

How the request reaches the servlet through tomcat: https://blog.csdn.net/sumengnan/article/details/111504047

 

2. The dispatchServlet will first query the processor mapper and return to the processor execution chain (including interceptors and handlers)

After reaching the servlet, it will eventually execute to the doDispatch method

The content of the getHandler method is as follows: according to the handlerMapping obtained at the beginning of the initialization to find the appropriate processor mapper one by one

Call the getHandlerInternal method of each implementation class of handlerMapping one by one, as shown in the figure:

Try to parse out the Handler, if it can, continue to execute, encapsulate the HandlerExecutionChain execution chain and return

Create an execution chain through new HandlerExecutionChain, and add an interceptor through addInterceptor, as shown in the figure:

HandlerMapping has a variety of implementation classes, which are used to handle different mapping configuration methods.

E.g:

  • BeanNameUrlHandlerMapping: Bean tag configuration processor method in xml. For example: <bean id="aController" name="/a.action" class="com.xxx.simpleController"/>
  • SimpleUrlHandlerMapping: The urlMap attribute of bean configuration in xml implements the mapping of request URL to controller (controller handler bean). <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"><property name="urlMap"><map><entry key="a.action" value-ref="aController"/></map ></property></bean>
  • RequestMappingHandlerMapping: The handler method used to process @RequestMapping annotations

  You can also customize the processor mapper HandlerMapping, but pay attention to the Order order (implement the Order interface or @Order annotation)

 

3. The dispatchServlet queries the corresponding processor adapter, the processor adapter calls the custom controller (the interceptor executes the interceptor before method), and returns the ModelAndView object (which contains the logical view), and then executes the interceptor post method

Continue to execute down the doDispatch method to the getHandlerAdapater method

The content of the getHandlerAdapter method is as follows: according to the handlerAdapter obtained at the beginning of the initialization to find a suitable processor adapter one by one

The HandlerAdapter interface has a variety of implementation classes as follows: they are used to call different Handlers

E.g:

  • HttpRequestHandlerAdapter is used to call the controller that implements the HttpRequestHandler interface
  • SimpleControllerHandlerAdapter is  used to call the controller that implements the Controller interface
  • RequestMappingHandlerAdapter is  used to call the @RequestMapping annotated controller

These three are commonly used, others can try if you are interested.

 

Three ways SpringMVC implements controller controller?

1. Implement the Controller interface (the return value is ModelAndView)

2. Implement the HttpRequestHandler interface ( no return value )

3. @Controller or @RestController annotation

 

If you want to customize the Adapter to call the controller, you can implement the HandlerAdapter interface, but pay attention to the Order order (implement the Order interface or @Order annotation)

The timing of calling the interceptor is as follows:

There is another key point here, that is, when the annotation @RequestMapping is used, how are the parameters of the reflection call parsed?

Reference: https://blog.csdn.net/sumengnan/article/details/113774179

4. dispatchServlet to query the corresponding view resolver and return View (real view)

Continue to execute down the doDispatch method to the processDispatchResult method

Continue to go down, if the handler returns to the mv view, it will enter the render method to perform rendering operations, otherwise skip

If the name of the logical view viewName is set in the ModelAndView object, look for the corresponding view resolver and return the view object

The implementation class of ViewResolver view resolver interface is as follows:

The implementation classes of the View view interface are as follows:

 

5. Render the View, and then execute the interceptor After method

Continue to go down and call the render method of the view interface

Execute interceptor after method

 

6. Return to the user

 

MVC custom configuration

three methods:

  1. Implement WebMvcConfigurer interface 
  2. Inherit the WebMvcConfigurerAdapter class (obsolete)
  3. Inherit the WebMvcConfigrutionSupport class

When in the springboot environment, it is best not to inherit the WebMvcConfigrutionSupport class. Because the springboot automatic configuration class WebMvcAutoConfiguration has @ConditionalOnMissingBean(WebMvcConfigurationSupport.class) annotation, springboot will not help us automatically configure it, we also need to manually configure various components, such as ResourceHandlers, otherwise it will cause a 404 when accessing static resources

Explanation of each method of WebMvcConfigurer interface, refer to: https://blog.csdn.net/sumengnan/article/details/109064789

1. 2. Implementing the WebMvcConfigurer interface and inheriting the WebMvcConfigurerAdapter class is actually an effect, but the second one is out of date.

@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/nihao").setViewName("success");
    }
    //……其他方法
}

3. Inherit the WebMvcConfigrutionSupport class

@Configuration
class MyMvcConfig extends WebMvcConfigurationSupport {
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/nihao").setViewName("success");
    }
    //……其他方法
}

Expansion:

When we are in the springboot environment , we can configure the following in application.protires:

# SPRING MVC
spring.mvc.locale=en_UK
spring.mvc.date-format=yyyy-MM-dd

 In fact, it is the configuration registered in the way of inheriting the WebMvcConfigrutionSupport class in the end .

Springboot encapsulates our configuration into the WebMvcProperties class

 

 

 

Guess you like

Origin blog.csdn.net/sumengnan/article/details/105381225