In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
4. The processing flow of HTTP request in Spring framework
After traversing the Web container and the Web application, the HTTP request will be delivered to the Spring framework, and we will continue to analyze the subsequent process. The connection between Web application and Spring MVC is completed through the configuration file mvc-servlet.xml, through which we define the core components and initialization configuration of Spring MVC, including controller Controller, view parser ViewResolver, view View and so on. Different components perform different functions, so let's take a look at these core components as usual before we introduce the Spring framework to handle the HTTP request process.
4.1 introduction to the core components of the Spring framework
When using Spring, we application developers come into contact with a variety of annotations, including @ Component, @ Controller, @ Service, @ Repository, etc., which are the core components of Spring. In addition, we will use auxiliary annotations such as @ RequestMapping, @ RequestParam, @ PathVariable, @ RequestBody and so on:
Model Model: encapsulates business data, mainly in the form of POJO. Controller Controller: mainly responsible for the scheduling and control of specific business processes, as well as calling business logic service Service. After the controller Controller processes the HTTP request, the ModelAndView object is returned to the front dispenser DispatcherServlet,ModelAndView, which contains the model Model object and the view View name. View View: responsible for rendering model Model data and providing forms for users to enter various business data. View parser ViewResolver: responsible for getting an instance object of a specific view View subclass based on a known view name. Processor mapping HandlerMapping: mainly responsible for the registration and lookup of the controller Controller, held by the application context ApplicationContext. In implementation, it has a member attribute handlerMap of type HashMap, where key is the URI information requested by HTTP, and value can be a string or a HandlerExecutionChain that handles the request. If it is a String, it is considered a Spring Bean name. Service Service: mainly responsible for implementing specific business logic.
Data storage object Repository: also known as data access object DAO (Data Access Object). No matter what development framework is used, most applications need to interact with the database. DAO encapsulates the operation of accessing the database and isolates the complexity related to SQL.
All HTTP requests arriving at the Spring MVC are uniformly distributed by the pre-dispenser DispatcherServlet, which needs to be located with the help of the processor mapping HandlerMapping before distributing the request to a specific controller Controller, roughly as follows:
The Web container listens on the specific port of the host, and whenever a request arrives, the Web container will eventually call the service method of Servlet to process the HTTP request. In Spring Web applications, the Servlet that receives HTTP requests is the pre-dispenser DispatcherServlet. Determine the HTTP request type in the service method of the pre-dispenser DispatcherServlet, including: GET, POST, PUT, PATCH, and so on, and then decide to call doGet (), doPost (), doPut (), and so on. The proce***equest () method is executed in the doGet (), doPost (), doPut (), and other methods to complete the initialization of the request context. Call the doService () method to further execute the doDispatch () method. Get the mappedHandler and HandlerAdapter of the HTTP request in the doDispatch () method, and then initiate a call to the business controller Controller and the subsequent process, waiting for the processing result before building the response data. 4.2 the flow of the Spring framework for processing HTTP requests
After receiving the HTTP request, the pre-dispenser DispatcherServlet will find the appropriate controller Controller to process the request. It obtains the URI by parsing the URL of the HTTP request, and then obtains the processor Handler and the processor interceptor HandlerInterceptor from the processor mapping HandlerMapping according to the URI, and finally returns in the form of HandlerExecutionChain. The pre-dispenser DispatcherServlet selects the appropriate adapter HandlerAdapter according to the obtained processor Handler. If the adapter HandlerAdapter is successfully obtained, the method preHandler () of its interceptor takes precedence before the processor Handler is called. The method preHandler () extracts the data from the HTTP request and populates it into the input parameters of the processor Handler, and then starts calling the processor Handler (that is, the controller Controller) related methods. After the controller Controller execution is complete, the forward dispenser DispatcherServlet returns a model and view name object ModelAndView. The pre-dispenser DispatchServlet selects the appropriate view parser ViewResolver based on the model and the view name object ModelAndView, provided that the view parser is already registered with the Spring IOC container. The view parser ViewResolver will get a specific view View based on the view name specified in the ModelAndView. The pre-dispenser DispatchServlet populates the model data into the view and returns the rendering result to the client.
In the process of populating the processor Handler input parameters, Spring also does some preprocessing work based on the configuration:
HttpMessageConveter: converts request messages (JSON\ XML and other formatted data) into Java objects. Data conversion: do type conversion on the data in the HTTP request, for example, convert String to Integer, Double, etc. Data formatting: formatting specific data in an HTTP request, such as converting a string to a formatted number or formatting a date, etc. Data validation: verify the validity of the data (length, format, etc.), and the verification results are stored in BindingResult or Error. 4.3 differences of HTTP request processing flow under different application architectures
Spring Web application architecture has experienced many stages of development. At first, the mainstream front-end view technology is JSP, and on this basis, the three Musketeer framework SSH (Struts\ Spring\ Hibernate) has evolved, but at this time, the front and back ends are actually coupled together, whether it is JSP or SSH. In the previous Spring framework process of HTTP requests, we must rely on the view parser ViewResolver and view View.
It has been more than 15 years since the birth of Spring, the evolution of its associated back-end technology is actually not as fast as the front-end, the main reason is that the front-end requirements are becoming more and more rich and diverse, and the development workload and complexity of the front-end view layer are increasing. In this context, more and more front-end engineering solutions are emerging, the most effective of which is front-end separation, from AngularJS\ Backbone.js to now React\ Vue and so on. In this front-end separation architecture, the front end is all composed of static resources (HTML\ Javascript\ CSS), which can be independently deployed in the Web server, so that the Spring framework no longer needs to deal with view-related content, and the controller Controller no longer returns ModelAndView, only needs to feedback the model data.
The main value of this article is to help you sort out the end-to-end framework of the whole process, that is, the global perspective or God perspective. With this framework, we can according to our own needs to find clues to find the relevant node information to study, so as not to fall into the details can not find the direction. Of course, considering that our work and study conditions are different and the problems we usually encounter are also different, this article can not cover the problems encountered by everyone. You are welcome to leave messages and questions, and you are welcome to follow my blog or the official name "IT veteran brother" to communicate and interact. I will try my best to answer your questions as soon as possible. Thank you!
The index of other articles in this series is as follows:
Diagram Spring:HTTP request processing flow and mechanism [1] diagram Spring:HTTP request processing flow and mechanism [2] diagram Spring:HTTP request processing flow and mechanism [3] diagram Spring:HTTP request processing flow and mechanism [5]
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.