Spring MVC source in-depth profiling execution process

Source: Internet
Author: User
Spring MVC source in-depth profiling execution process

Spring MVC is now the most popular MVC framework, and many people say it is an excellent framework. In essence, because spring MVC joins annotations, annotations make spring MVC qualitative, which makes development efficiency soar. And it is a small part of spring itself, so let's not like Struts2, need plug-in support. In other words, they are seamlessly connected. But no matter how good the framework is, they are always built on listener, Servlets, and filter these server container components. The visible foundation is particularly important.

First, if we want to add spring MVC support to a Web project, we want to configure the following code in XML:

Among them, Org.springframework.web.servlet.DispatcherServlet is the starting point of the whole spring MVC framework. Similar to adding struts. Struts is a pointcut through filter, and spring MVC uses servlets. The principle is actually the same, all play the role of intercepting user request. Filter is an instance that is generated when the server is started. If the servlet does not set Load-on-startup, the instance is generated for the first access. Dispatcherservlet requires the instance to be generated when the server is started.

Below we will parse the general process of spring MVC through the source code:

When we send a request, Dispatcherservlet will intercept it, and for the servlet. The service method is the core of the processing request, but this method is not defined in Dispatcherservlet. We look through the parent class Frameworkservlet:

The ProcessRequest (Request,response) method call, in this method, calls the Doservice (Request,response). At this time Doservice is the Doservice method in the above Dispatcherservlet. The Dodispatcher method is also called in Doservice. Then it can be seen that Dodispatcher is the core method of Dispatcherservlet processing.

The following is the source code for the Dodispatcher method:

First there are several classes that need to be declared, Modelandview, Handlerexecutionchain, handlermapping, Handlermethod, and Handleradapter.

1. Handlermethod (Org.springframework.web.method.HandlerMethod), this class is a method object in which a Bean object and the bean object are stored.

2, handlermapping, the function is through request object, obtains the corresponding Handlermethod object.

3. The Handlerexecutionchain function is the Handlermethod object returned by adding the Interceptor Interceptor wrapper handlermapping. Let the pending method object and the interceptor be called a whole, that is, the execution chain.

4, Modelandview, as the name implies. Model, the M,view of MVC, is the V of MVC. It is the data and view information that the object holds.

5, Handleradapter: The role for the specific processing of handlermethod, that is, through it calls a method.

The following analysis:

In the Dodispatcher method, the handler is first obtained through the GetHandler method, as follows:

Traverse all handlermapping and invoke the GetHandler method of each Handlermapping object, and Handlermapping is the interface, Where abstracthandlermapping implements the Handlermapping interface in this abstract class, and implements the GetHandler method, as follows:

Where Object handler = gethandlerinternal (request); To get the Handlermethod object, the source code for this method is as follows:

After acquiring the Handlermethod object, the Handlermethod object is encapsulated with the Interceptor interceptor via Gethandlerexecutionchain (handler, request). Become the object of the Handlerexecutionchain. This is the source flow of the object that the Mappedhandler reference points to in the Dodispatch method.
Once the Handlerexecutionchain object is obtained, it can be used to obtain the Handlermethod object that was previously encapsulated in the object. by GetHandler ().

The object of the concrete implementation class of the Handleradapter interface is to invoke a method that is called by reflection to invoke a method that corresponds to an instance of a class encapsulated in the Handlermethod object.

The return value is an object of type Modelandview, which does not have to be said. There are two method invocations before and after the Hanle method:

The function is to intercept by the Interceptor interceptor in the execution chain. function is similar to filter.

Summary as shown:

It should also be noted that if we want to use spring MVC, we must have a configuration file similar to that of Spring Applicationcontext.xml (the default name is Servletname-servlet.xml, The servletname here refers to the configuration file for the servlet's name attribute configured in Web. Xml. It does not need to say much, similar to spring's applicationcontext.xml, as the core file of the IOC container, all bean components are created through it. is also at the heart of the IOC.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.