Spring Architecture Demystified - MVC

1. Spring MVC flow chart
2. Spring MVC data binding and conversion
 
1. Overview of the Spring MVC process
     I used to use Struts for MVC projects, and then combined with Spring for bean management, AOP, and transaction configuration, but since the launch of Spring MVC, it has greatly simplified the configuration of MVC, and has made a qualitative leap in the development of web MVC projects. . Let's briefly look at the main process of Spring MVC.
1. When a user sends a web request through a browser, the application server, such as tomcat, is first received. After a series of actions such as protocol parsing, the application server passes the request object HttpServletRequest to DispatcherServlet, which is the core entry of Spring MVC.
 
2. After the DispatcherServlet receives the request request, it will set some request-related attributes in the doService method, such as applicationContext, LocalResolver that supports internationalization, etc., and then pass the request to the request processing adapter
 
3. Spring will map the request uri with the request handler during the initializing of the applicationContext. During the runtime, the request processing adapter HandlerAdapter finds the corresponding handler HandlerMethod through the request object . The Handler in Spring MVC is of type Object, which is actually The @RequestMapping method is configured on the Controller, so the HandlerMethod object is provided by default as the request processor.
 
4. Handler processes the request, that is, after calling the corresponding business method logic, it will return the ModelAndView view object, and then call the applyPostHandle method of HandlerExecutionChain to process the view object
 
5. Spring MVC uses the view parser to parse the ModelAndView object, and then generates the corresponding page to the DispatcherServlet, which writes the response back to the client, thus basically completing the processing of the entire request.
 
Let's take a look at the class diagram structure of DispatcherServlet
 
     DispatcherServlet inherits FrameworkServlet, FrameworkServlet implements the servlet interface, and at the same time implements Spring's ApplicationContextAware to have the ability to set the context.
    
     DispatcherServlet aggregates HandlerExecutionChain and HandlerAdapter, in which HandlerExecutionChain aggregates HandlerInterceptor. The main function of HandlerExecutionChain is to execute injected interceptors before and after executing specific business methods.
 
     HandlerAdapter is an adapter for a handler or a selector. Readers may ask, isn't there a handler in the HandlerExecutionChain, why not use the HandlerExecutionChain to execute the handler directly instead of setting up an Adapter? In fact, it is because Spring MVC supports a variety of processing methods, such as writing a class directly, adding the annotation @RequestMapping to the method to generate a processor, and also supporting native Servlet and other methods, so I know it when processing a request. The method of executing business processing, but what kind of context should be set for specific execution, and the preparations that need to be done before execution are different. So there is the Adapter layer. Use HandlerAdapter.support(Object handler) to determine whether the request handler can be executed.
 
     When the DispatcherServlet determines which HandlerAdapter to use, it calls the handle() method to execute the business logic, and then returns the ModelAndView object for the view layer to use.
 
     After getting the ModelAndView, process the view object through the processDispatchResult( processedRequest , response , mappedHandler , mv , dispatchException ); method. Here, the ViewResolver will parse the ModelAndView into a View object, and then call the render method of the View to find the response object to be returned and generate it. returned to the user.
 
Second, Spring MVC data binding and conversion
     The binding processing of Spring MVC's request data is performed when the Handler method is executed. Let's take a look at the class diagram structure related to data binding
The main classes are marked with dark yellow in the figure. Next, we will analyze these classes in detail.
1. RequestMappingHandlerAdapter: It specifically executes the handler in Spring MVC. It executes the Handler through reflection in the invokeHanlderMethod method. Before the specific execution, it converts and binds the data passed by the request. From the class diagram, you can know that it is data bound through WebDataBinderFactory.
2. DataBinder is the core component of Spring MVC data binding. It performs data type conversion and data formatting through the ConversionService component in the assembly context. It can be seen that Converters are aggregated in its implementation class GenericConversionService. Converters are assembled with a gloBalConverters Set collection and a Map collection. When it is actually running, there will be a bunch of conversion classes, of which ObjectToStringConverter is one of them. In fact, the JDK itself also Provides a similar conversion tool is PropertyEditor, but its core function is to convert a string into an object, which is not suitable for conversion between two objects, and for the context information of the source object (annotations, host class structure, etc. ) is not sensitive and cannot implement some logic through this information during conversion, so Spring has created a new conversion system.
 
The following figure is a schematic diagram of the working mechanism of DataBinder

 
 
 
 

 

 

Guess you like

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