SPRINGMVC Core distributor Dispatcherservlet Analysis [included source analysis] Directory
- Objective
- Dispatcherservlet initialization Process
- Dispatcherservlet processing the request process
- Summarize
- Resources
Objective
SPRINGMVC is currently one of the mainstream web MVC frameworks.
If a classmate is unfamiliar with it, then please refer to it's Getting started blog:http://www.cnblogs.com/fangjian0423/p/springmvc-introduction.html
This article analyzes the initialization process of SPRINGMVC's core dispatcher Dispatcherservlet and the process of processing requests, allowing the reader to understand the role of this portal servlet.
Dispatcherservlet initialization Process
Before we analyze Dispatcherservlet, we first look at the inheritance of Dispatcherservlet.
Httpserlvetbean inherits from HttpServlet.
Httpservletbean has covered the Init method and has done some processing on the initialization process. Let's see what the Init method does:
<Servlet><Servlet-name>dispatcher</Servlet-name><Servlet-class>org.springframework.web.servlet.dispatcherservlet</Servlet-class><Load-on-startup>1</Load-on-startup><Init-param><Param-name>contextconfiglocation</param-name> < param-value>classpath:springconfig/dispatcher-servlet.xml </param-value> </init-param ></servlet>< servlet-mapping> <servlet-name >dispatcher</servlet-name> < url-pattern>/</url-pattern> Span class= "Hljs-tag" ></SERVLET-MAPPING>
For example above this configuration, passed the contextconfiglocation parameter, then constructs the Beanwrapper, here uses the Beanwrapper, has 2 reasons: 1. The Contextconfiglocation property is defined in Frameworkservlet, and 2 is not defined in Httpservletbean. With the injection feature of spring, only the Setpropertyvalues method is called to set the Contextconfiglocation property to the corresponding instance, that is, the property is initialized in a dependency injection manner.
Then set the Contextconfiglocation property in Dispatcherservlet (defined in Frameworkservlet) The contextconfiglocation parameter read in Web. XML, which is used to construct the SPRINGMVC container context.
See below Frameworkservlet This class, Frameworkservlet inherit from Httpservletbean.
First look at the Initservletbean method of this class of override:
Next look at the concrete implementation logic of the Initwebapplicationcontext method:
The root context here is the context generated by the contextconfiglocation path in the Contextloaderlistener listener configured in Web. Xml.
<context-param> <param-name>contextconfiglocation</param-name> <param-value>classpath: Springconfig/applicationcontext.xml</param-value> </context-param>< listener> <listener-class> Org.springframework.web.context.contextloaderlistener</ listener-class> </LISTENER>
For example, the root context generated in this configuration file is based on the XML file under Classpath:springconfig/applicationcontext.xml.
Finally look at the Dispatcherservlet.
Dispatcherservlet covered the Onrefresh method in Frameworkservlet:
It is clear that the Initstrategies method internally initializes the implementation classes for each policy interface.
For example, exception handling initialization Inithandlerexceptionresolvers method: Springmvc exception handling mechanism detailed
View handling Initialization Initviewresolvers method: SPRINGMVC View mechanism
Request Map Processing initializes the Inithandlermappings method: How to find the correct controller when SPRINGMVC request is detailed
Summarize the role of each servlet:
1. Httpservletbean
Basically do some initialization work, set the parameters configured in Web. XML to the servlet. For example, the child label of the servlet tag init-param the parameters configured in the tag.
2. Frameworkservlet
Associates a servlet with the spring container context. In fact, this is the initialization of the Frameworkservlet property Webapplicationcontext, which represents the SPRINGMVC context, which has a parent class context that The container context that is initialized by the Contextloaderlistener listener configured in Web. Xml.
3. Dispatcherservlet
Initializes the implementation class for each feature. such as exception handling, view processing, request mapping processing and so on.
Dispatcherservlet processing the request process
Before parsing the Dispatcherservlet processing request process, we review the servlet's handling of the request.
HttpServlet provides a service method for processing requests, the service uses template design patterns, internally for the HTTP GET method calls the Doget method, and the HTTP POST method calls the Dopost method ....
Enter the ProcessRequest method to see:
The type of listener registered is the Applicationlistener interface type.
Continue to see the Dispatcherservlet Doservice method:
The end is the Dodispatch method.
The Dodispatch method features a simple description:
First find the Handlermethod (with the method reflection property, which corresponds to the controller) according to the path of the request, then match the path corresponding to the Interceptor, A Handlerexecutionchain object is constructed with Handlermethod and interceptors. The acquisition of the Handlerexecutionchain object is obtained through the methods provided by the Handlermapping interface. With Handlerexecutionchain, the Handleradapter object is processed to get the Modelandview object, handlermethod the internal handle, Use a variety of handlermethodargumentresolver to implement class handling Handlermethod parameters, and use various Handlermethodreturnvaluehandler to implement class processing return values. The final return value is processed into the Modelandview object, and the exception that occurs during this period is handled by the Handlerexceptionresolver interface implementation class.
Summarize
This paper analyzes the role of SPRINGMVC-Dispatcherservlet, the parent class Httpservletbean and Frameworkservlet.
The design of the SPRINGMVC is completely different from the Struts2, and Struts2 is a completely isolated and decoupled mechanism for web containers, and SPRINGMVC is designed based on the most basic request and response.
It is inevitable that the reader will be able to specify the error.
Resources
http://my.oschina.net/lichhao/blog/102315
http://my.oschina.net/lichhao/blog/104943
http://jinnianshilongnian.iteye.com/blog/1602617
Springmvc Core Dispatcher Dispatcherservlet analysis [with source code analysis]