The process of SpringMVC and SpringBoot responding to requests

SpringMVC is an MVC model based on Servlet. Model: one or more javabean objects, used to store data and business logic; View: one or more jsp pages, which get the data submitted by the controller to provide data display for the model; Controller: a or multiple servlet objects, controlled based on requests submitted by the view.

 The execution process of SpringMVC is as follows.

  1. The user clicks on a request path to initiate an HTTP request, which will be submitted to DispatcherServlet (front-end controller);
  2. One or more HandlerMapping (processor mappers) are requested by DispatcherServlet and an execution chain (HandlerExecutionChain) is returned.
  3. DispatcherServlet sends the Handler information returned by the execution chain to HandlerAdapter (processor adapter);
  4. HandlerAdapter finds and executes the corresponding Handler (often called Controller) based on Handler information;
  5. After Handler is executed, it will return to HandlerAdapter a ModelAndView object (the underlying object of Spring MVC, including Model data model and View view information);
  6. After HandlerAdapter receives the ModelAndView object, it returns it to DispatcherServlet;
  7. After DispatcherServlet receives the ModelAndView object, it will request ViewResolver (view resolver) to parse the view;
  8. ViewResolver matches the corresponding view result based on View information and returns it to DispatcherServlet;
  9. After receiving the specific View, DispatcherServlet renders the view, fills the model data in the Model into the request field in the View, and generates the final View;
  10. The view is responsible for displaying the results to the browser (client).

Spring MVC interface

The components involved in Spring MVC are DispatcherServlet (front-end controller), HandlerMapping (processor mapper), HandlerAdapter (processor adapter), Handler (processor), ViewResolver (view resolver) and View (view). The functions of each component are described below.

1)DispatcherServlet

DispatcherServlet is the front-end controller. As can be seen from Figure 1, all requests of Spring MVC must be uniformly distributed through DispatcherServlet. DispatcherServlet is equivalent to a forwarder or central processor, controlling the execution of the entire process and uniformly scheduling each component to reduce the coupling between components and facilitate the expansion between components.

2) Handler Mapping

HandlerMapping is a handler mapper. Its function is to find matching handler (Handler) information through annotations or XML configuration according to the requested URL path.

3)HandlerAdapter

HandlerAdapter is a processor adapter. Its function is to execute the relevant handler (Handler) according to specific rules based on the handler (Handler) information found by the mapper.

4)Handler

Handler is a processor and plays the same role as Java Servlet. Its function is to execute relevant request processing logic, return corresponding data and view information, and encapsulate it into the ModelAndView object.

5)View Resolver

View Resolver is a view resolver. Its function is to perform parsing operations and parse the logical view name into a real view View through the View information in the ModelAndView object (such as returning a real JSP page through a JSP path).

6)View

View is a view, itself an interface, and the implementation class supports different View types (JSP, FreeMarker, Excel, etc.).

Among the above components, developers need to develop the handler (Handler, often called Controller) and the view (View). In layman's terms, it is necessary to develop the specific code logic to handle the request, as well as the final interface displayed to the user.

SpringBoot startup process:

1. First find the run() method from main, and create a new SpringApplication object before executing the run() method.
2. Enter the run() method, create the application listener SpringApplicationRunListeners and start listening.
3. Then load the SpringBoot configuration environment (ConfigurableEnvironment), and then Add the configuration environment (Environment) to the listening object
4. Then load the application context (ConfigurableApplicationContext) as the return object of the run method.
5. Finally create the Spring container, refreshContext (context), and implement automatic starter configuration and bean instantiation.
Container containment relationship:

 

The main process of request processing
1. Execution before filter chain chain.doFilter (the filter is in the Server container, such as Tomcat);
2. Execution of the preHandle method of the interceptor chain;
3. Path mapping, parameter binding (parameter parsing, Parameter conversion, parameter verification);
4. Controller's specific method execution;
5. Return value processing (including information conversion);
6. Interceptor chain postHandle method execution;
7. Exception parser handles exceptions (@ControllerAdvice, custom exceptions The parsers are all executed here);
8. View parsing and rendering;
9. Execution of the interceptor chain afterCompletion method;
10. Execution after the filter chain chain.doFilter.


A complete SpringBoot request process:

1. Request initiated by the front end

2. According to the path, Springboot will load the corresponding Controller for interception.

3. After interception processing, jump to the corresponding Service processing layer

4. Jump to ServiceImplement (service implementation class)

5. When executing serviceimplement, the Dao layer will be loaded and the database will be operated.

6. Then jump to the Dao layer implementation class

7. The execution will jump to the mapper layer

8. Then MallMapper will continue to find the corresponding mapper.xml configuration file, and then jump to step 4 to continue execution. After the execution is completed, the result will be returned to step 1, and then the data will be returned in the form of JSON. page, and returns the status code at the same time. If it is normal, it will return 200, and then it will return to step 1 for query and judgment. If the judgment is normal. The front end traverses and displays based on data

Guess you like

Origin blog.csdn.net/a154555/article/details/128531136
Recommended