Talking about springmvc source code: the working principle of the adapter

Introduce the powerful front-end controller in springmvc in web development to decouple the original control logic and model logic, which is not only beautiful, but also has clear intentions and can increase reusability.
It turns out that if this mode is not used and the control logic and model logic are written in jsp, then:
1. The content of jsp is very confusing and difficult to understand. This is not conducive to modification and maintenance.
2. After the jsp is transformed into a servlet, the content of the jsp is put into the service method of the servlet. In this method, there are not only
statements for controlling the process, but also statements for model logic, as well as statements for page generation, which is too messy.
This time, by explaining the four major components in springmvc, DispatcherServlet, handleMapping, HandlerAdapter, Handle (which can be understood as Controller) to explain the adapter in springmvc First, the role of the adapter is to
convert an interface into another interface that the user wants, the adapter Patterns make those classes with incompatible interfaces work together. The specific details about the adapter will not be explained in detail. Below we directly analyze the source code of the doPatch() method in DispatcherServlet (the doPatch() method is the essential method for dispatcherServlet to process the service() method)
insert image description hereinsert image description here

The HandlerExecutionChain class can be thought of as a "container", but in order to avoid confusion with the "container" in the Spring container we often say, it is more appropriate to use "holders". The design purpose of this "holder" is to hold some things. What are these things? It is a Handler and a set of HandlerInterceptors in a specific order. When processing a request, DispatcherServlet finds the corresponding processor mapping item (HandlerMapping, HandlerExecutionChain) according to the processor mapping configured in the corresponding configuration file after receiving the request. According to the configuration Mapping rules to find the corresponding handler (Handler) and interceptor. There are many different types of controller classes in the dispatcherservlet. After the mappinghandle, the corresponding controller has been found, so why do we need to add an additional layer of Handler after matching the Mapping corresponding to the request? In fact, this has something to do with the way our Controller is defined.
There are three ways to define a Controller in SpringMvc.

  • The most common one is through the @Controller annotation.

  • Implementing the Controller interface can also define a class as Controller

  • Implement the Servlet interface definition Controller.

Then the question at this time is: if there are so many implementation methods at the same time, how to execute the business method after DispatchServlet obtains the corresponding Mapping? One conceivable way is: IF/ELSE structure, so what if a new Controller definition method is added in the future? The method of DispatchServlet has to be modified, and this implementation method violates the principle of opening and closing the code. Therefore, Spring defines a unified Adapter class, and defines its own adapter class for each type of Controller. Different Controller definitions only need to obtain different adapter classes to complete the invocation of specific methods.
DispatcherServlte will match the controller passed by handlerMapping with the registered HandlerAdapter one by one to see which HandlerAdapter supports the controller type. If one of the HandlerAdapters is found to support the passed controller type, then the HandlerAdapter will Call your own handle method. The handle method uses the java reflection mechanism to execute the specific method of the controller to obtain the ModelAndView. For example, the SimpleControllerHandlerAdapter is a controller that supports the implementation of the controller interface. If the controller you write yourself implements the controller interface, then the SimpleControllerHandlerAdapter will go to Execute the specific method in the controller written by yourself to complete the request.

Guess you like

Origin blog.csdn.net/qq_52696089/article/details/121204669
Recommended