The operation mechanism of DispatcherServlet in Spring
DispatcherServlet is the core servlet in the Spring's web framework (Springweb). "Spring Web Framework - like other web frameworks - is a request-driven web framework, which is designed to distribute requests to the controller Servlet, which also provides other functions to help web application development. "----" Spring Framework Development Reference Manual (Chinese) "and this servlet in the Springweb frame is org.springframework.web.servlet.dispatcherServlet. The inheritance relationship of this servlet is shown in the following figure: Springweb first packages the traditional HTTPSERVLET abstraction class into bean; FrameworkServlet abstract some basic behaviors of servlets in the web frame, such as access to Application Context; the main job of DispatcherServlet is A Request is distributed to a suitable processor and draws the modelandView to the client to return to the client.
DispatcherServlet As a servlet, he must have two main methods: init () and doservice ().
An init () initialization. DispatcherServlet inheritance hierarchy init realize () method located HttpServletBean in, HttpServletBean first call initBeanWrapper (), initialize BeanWrapper, then call the abstract method initServletBean (), implementation of this method is located in a subclass FrameworkServlet in; FramewordServlet in initServletBean () method calls initWebApplicationContext (), initialize WebApplicationContext, then the same call his abstract method initFrameworkServlet (), and the implementation of the abstract method is in the final DispatcherServlet in; DispatcherServlet in initFrameworkServet () will in turn initiate Multipar (for file uploading) parsing Localization information parser, the subject parser processor mapping, etc. So the initialization order of DispatcherServlet is init (); initbeanwrapper ();
INITSERVLETBEANINITWEBAPPLICATIONCONTEXT ()
INITMULTIPARTRESOLVER (); initthemeresolver (); inittHemeresolver (); inithandlermappings (); inithandleradapters (); inithandlerexceptionResolvers (); initViewResolvers ();
Two DOSERVICE () Processing Request. In the DispatcherServlet, whether it is submitted by the POST method or the GET method, it will eventually be handed over to DOSERVICE (). The processing logic in doservice () is more than six steps: 1.IF (Request is Multipart, i.e., file upload), resolution, and package into multipletpservletRequest2.maptedHandler = GetHandler (Request) to get the appropriate processor 3 according to Request. PREHANDLE method for invoking all interceptors registered 4. Call processor handleradapter HA = new getHandleRadapter ()); ModelandView MV = Ha.Handle (Req, Res, mappedHandler.getHandler ()) // This use Adapter Pattern 5. Posthandle method for all interceptors that call registration 6. Draw MV may not be like struts, Hibernate is the most widely used, but he is comprehensive, lightweight, enough flexible, easy to replace, easy to expand of. Springweb is a part of SpringFramework, and DispatcherServlet is a small part of Springweb. To understand Spring and his design ideas, it is still a long way to go. I hope that before the new road, we have already traveled this road. The road is long and the road is long, I will go up and down. Continue research ing ... to beundine ...
References Spring official website http://www.springframework.org/Spring API http://www.springframework.org/docs/api/index.htmlSpring Reference Documentation http://www.springframework.org/docs/reference/ Index.htmlspring Framework Development Reference Manual (Chinese Version) http://www.jactiongroup.net/reference/html/index.html
Just seen from Spring Web Framework 0.4.pdf (by floater@cjsdn.com) to see the following two sequence graphs, more explanation: