Illustration Spring: mechanism and process flow of the HTTP request [4]

4. HTTP request processing flow in the Spring Framework

After crossing a Web container and Web applications, HTTP requests will be delivered to the Spring framework, we continue to analyze the follow-up process. Spring MVC Web application and convergence is through configuration files mvc-servlet.xml done, we constitute a Spring MVC various core components and initial configuration is defined by this profile, including: controller Controller, view resolver ViewResolver, view view and so on. The different components are to assume different functions, before the introduction of the Spring framework to handle HTTP request process, as usual, we first look at these core components.

4.1 Spring Framework core components Profile

Our application developers using Spring contact the most is the variety of annotations, including: @ Component, @ Controller, @ Service, @ Repository, etc., these are the core components of Spring. In addition, we will use the @ RequestMapping, @ RequestParam, @ PathVariable, @ RequestBody other auxiliary notes:

  • Model Model: encapsulating the service data, mainly in the form POJO.
  • Controller Controller: is responsible for scheduling control specific business processes, business logic and call service Service. The Controller returns after processing the HTTP request object to the front ModelAndView dispenser DispatcherServlet, ModelAndView contains the model and view objects Model View name.
  • View View: responsible for rendering presentation model Model data, and provide a variety of business forms for users to enter data.
  • View resolver ViewResolver: View is responsible for retrieving a particular view of an object instance of a subclass of the view in accordance with known names.
  • Processor mapping HandlerMapping: Controller Controller is responsible for the registration and lookup, held by the application context ApplicationContext. It has a specific implementation HashMap <String, Object> type of member attributes handlerMap, wherein the key information is the URI of the HTTP request, value can be a string or HandlerExecutionChain processing request. If a String, it is regarded as Spring Bean name.
  • Services Service: mainly responsible for the achievement of specific business logic.
  • Data storage objects Repository: also known as Data Access Objects DAO (Data Access Object), no matter what development framework adopted, most applications need to interact with the database, DAO is to do a package to access the database operation, isolate the SQL-related complexity.

DispatcherServlet distribution request

Arrival of Spring MVC all HTTP requests by the pre-distributor DispatcherServlet unified distribution, before a request to the specific needs of the Controller With HandlerMapping handler mapping to locate, roughly as follows:

  • Web container host-specific port monitor, whenever there is a request arrived, the Web container will eventually call the service method of the Servlet HTTP request handling. In the Spring Web application, Servlet HTTP request received is pre-distributor DispatcherServlet.
  • Analyzing HTTP request type, the method comprising pre-service dispenser DispatcherServlet of: GET, POST, PUT, PATCH and the like, and then decide to call doGet (), doPost (), doPut () method and the like.
  • In doGet (), doPost (), doPut () method is performed like the processRequest () method to complete the initialization of the request context.
  • Call doService () method, the further implementation of doDispatch () method.
  • Acquires the doDispatch () method and HandlerAdapter is mappedHandler HTTP request, and then initiates the subsequent call flow of the Controller's service controller, and then wait for the processing result in response to data constructs.

4.2 Spring Framework HTTP request processing flow

Spring framework for handling HTTP

  • After the pre-distributor DispatcherServlet HTTP request is received, the controller will find the appropriate Controller to process the request, the URL which is obtained by parsing the URI of the HTTP request, then the processor Handler is obtained corresponding to the request from the processor which according to the URI mapping HandlerMapping and a processor interceptors HandlerInterceptor, and finally return to HandlerExecutionChain form.
  • Pre distributor DispatcherServlet Select a suitable adapter processor according HandlerAdapter obtained Handler. If successful adapter HandlerAdapter, its interceptor method preHandler () takes precedence before calling processor Handler.
  • Method preHandler () to extract the data filled into the HTTP request into parameters which processor Handler then starts Handler call processor (i.e., controller Controller) related methods.
  • After the completion of the implementation of the Controller, return to the pre-distributor DispatcherServlet a model and view subjects ModelAndView.
  • Pre-distributor DispatchServlet select the appropriate view resolver ViewResolver according to model and view subjects ModelAndView, the premise must be registered to view resolver among Spring IOC container.
  • View resolvers ViewResolver obtained in accordance with a particular view View inside ModelAndView specified view name.
  • Pre distributor DispatchServlet filled into the model view data which then render the results returned to the client.

在填充处理器 Handler 入参的过程中,Spring 还会根据配置做些预处理工作:

  • HttpMessageConveter:将请求消息(JSON\XML 等格式数据)转换成 Java 对象。
  • 数据转换:对 HTTP 请求中的数据做类型转换,例如:将 String 转换成 Integer、Double 等。
  • 数据格式化:对HTTP 请求中的特定数据做格式化,例如将字符串转换成格式化数字或格式化日期等。
  • 数据验证:验证数据的有效性(长度、格式等),验证结果存储到 BindingResult 或 Error 当中。

4.3 不同应用架构下 HTTP 请求处理流程的区别

Spring Web 应用架构经历了多个阶段的发展,最初主流的前端视图技术就是 JSP,在此基础上又演化出了三剑客框架 SSH(Struts\Spring\Hibernate),但这时候前后端其实还是耦合在一起的,不管是 JSP 还是 SSH,在前面 Spring 框架处理 HTTP 请求的流程中,必须要依赖视图解析器 ViewResolver 和视图 View。

从 Spring 诞生到现在已经15年多了,它关联的后端技术演化其实没有前端那么快,主要原因就是前端需求越来越丰富多样,前端视图层的开发工作量和复杂度不断增加。在这样的背景之下,越来越多的前端工程化解决方案涌现,其中最有成效的就是前后端分离,从 AngularJS\Backbone.js 到现在 React\Vue 等。在这种前后端分离架构下,前端就全部由静态资源(HTML\Javascript\CSS)等构成,可以独立部署在 Web 服务器当中,这样 Spring 框架就不需要再处理视图相关的内容,控制器 Controller 不再返回 ModelAndView,只需要反馈模型数据了。

Spring Process 2

The main value of this paper is to help you sort out the end of the whole process framework, that is, we often say that the global perspective or the perspective of God. With this framework After that, we can find what you want to find information related to the nodes according to their needs to study learning, and will not fall into the details not find the direction. Of course, taking into account the work of each of us to learn different situations, problems usually encountered is different, this article can not cover all problems encountered, welcome to leave a message to ask questions, I also welcome the attention of the public micro-channel number "IT veteran brother." interaction, I will try to answer your questions as soon as possible, thank you!

Other articles in this series, the index is as follows:

Guess you like

Origin www.cnblogs.com/itlaobingge/p/11963351.html