First take a look at the Web application deployment initialization process (Web application deployement), the official documentation notes:
WEB Application Deployment
When a Web application was deployed into a container, the following steps must was performed, in this order, before the Web Application begins processing client requests.
Instantiate an instance of all event listener identified by a <listener> element in the deployment descriptor.
For instantiated listener instances this implement Servletcontextlistener, call the Contextinitialized () method.
Instantiate an instance of all filter identified by a <filter> element in the deployment descriptor and call each Filter instance ' s init () method.
Instantiate an instance of all Servlets identified by a <servlet> element that includes a <load-on-startup> element in the order defined by the Load-onstartup
element values, and call each servlet instance ' s init () method.
Broadly speaking:
Web application Deployment: When a Web application is deployed to a container (EG.TOMCAT), the following steps are executed sequentially before the Web app starts processing the client request:
1. Initialize each of the listener in the app deployment profile.
2. Initialize the Servletcontextlistener implementation class and call the Contextinitialized () method.
3. Initialize each filter in the application deployment profile and execute each of the Init () methods.
4. Initialize the servlet in order <load-on-startup>, and execute the init () method.
Summary: Initialize Lisener First, then filter, and finally servlet
Common SPRINGMVC configurations:
<web-app> <display-name>web application</display-name> <!--global variable configuration--<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationcontext-*. Xml</param-value> </context-param> <!--listener--<listener> <listener-class> Org.springframework.web.context.contextloaderlistener</listener-class> </listener> <!-- Filter--> to solve garbled problems <filter> <filter-name>CharacterEncodingFilter</filter-name> < Filter-class>org.springframework.web.filter.characterencodingfilter</filter-class> <init-param> & lt;param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> < ;/filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> < Url-pattern>/*</url-pattern> </filter-mapping> <!--restful front-end controller-<servlet> <servlet-name>mvc</servlet-name> <servlet-class> Org.springframework.web.servlet.dispatcherservlet</servlet-class> <init-param> <param-name> Contextconfiglocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> </ init-param> </servlet> <servlet-mapping> <servlet-name>mvc</servlet-name> <url-patte Rn>/</url-pattern> </servlet-mapping></web-app>
Dispatchservlet Instructions for use:
The class diagram is as follows:
It can be seen that the Dispatchservlet class indirectly implements the Servlet interface, so it is still essentially a servlet. Dispatchservlet class design is very clever, the upper parent class
Different degree of implementation of the relevant interface part of the method, and set aside related methods for subclass coverage, the invariant part of the unified implementation, the change of partial reservation method for sub-class implementation.
Dispatchservlet class Initialization procedure function call graph:
From the logic of class diagram and related initialization function invocation, the initialization process of dispatchservlet will make the template method, its parent class accomplish different unification work, and reserve the related method for subclass
Overwrite to complete the different variable work. The essence of the Dispatchservlet class is a servlet that invokes the associated init (servletconfig) when the Web application is servelt initialized after it is deployed to the container.
method, so the initialization process for the Dispatchservlet class is also initiated by the method. Which frameworkservlet the Initservletbean () method in the abstract class, Initwebapplicationcontext ()
Method and the Onfresh () method in the Dispatchservlet class.
Frameworkservlet Initservletbean () method source code, the method overrides the Frameworkservlet abstract class in the execution, finally, Initxxxcontext Word appeared
initWebApplicationContext()
Method is first fetched from the ServletContext
ContextLoaderListener
root container object reference that was completed by the initialization and put into the
(Because creating a child container must pass in the parent container as an argument), and then after the layer call, the createWebApplicationContext()
container is created in the end,
The main code of the method is as follows:
Okay, here's the initialization done.
Dispatchservlet Processing Request Flow:
/** * Process the actual dispatching to the handler. * <p>the handler'll be obtained by applying the servlet's handlermappings in order. * The Handleradapter'll be obtained by querying the servlet's installed handleradapters * To find the first that SUP Ports the handler class. * <p>all HTTP methods is handled by this method. It's up to handleradapters or handlers * themselves to decide which methods is acceptable. * @param request Current HTTP request * @param response Current HTTP response * @throws Exception in case of any K IND of processing Failure*/ protected voidDodispatch (httpservletrequest request, httpservletresponse response) throws Exception {HttpServletRequest Pro Cessedrequest=request; Handlerexecutionchain Mappedhandler=NULL; Boolean multipartrequestparsed=false; Webasyncmanager Asyncmanager=Webasyncutils.getasyncmanager (Request); Try{modelandview mv=NULL; Exception dispatchexception=NULL; Try{processedrequest=Checkmultipart (Request); Multipartrequestparsed= (Processedrequest! =request); //determine handler for the current request.Mappedhandler =gethandler (processedrequest); if(Mappedhandler = =NULL|| Mappedhandler.gethandler () = =NULL) {Nohandlerfound (processedrequest, response); return; } //determine handler adapter for the current request.Handleradapter ha =Gethandleradapter (Mappedhandler.gethandler ()); //Process last-modified Header, if supported by the handler.String method =Request.getmethod (); Boolean Isget="GET". Equals (method); if(Isget | |"HEAD". Equals (method)) { LongLastModified =ha.getlastmodified (Request, Mappedhandler.gethandler ()); if(logger.isdebugenabled ()) {Logger.debug ("last-modified value for ["+ Getrequesturi (Request) +"] is:"+lastmodified); } if(NewServletwebrequest (Request, Response). Checknotmodified (lastmodified) &&isget) { return; } } if(!Mappedhandler.applyprehandle (processedrequest, response)) { return; } //actually invoke the handler.MV =Ha.handle (processedrequest, Response, Mappedhandler.gethandler ()); if(asyncmanager.isconcurrenthandlingstarted ()) {return; } applydefaultviewname (Processedrequest, MV); Mappedhandler.applyposthandle (processedrequest, response, MV); } Catch(Exception ex) {dispatchexception=ex; } Catch(Throwable err) {//as of 4.3, we ' re processing Errors thrown from handler methods as well,//making them available for @ExceptionHandler methods and other scenarios.Dispatchexception =NewNestedservletexception ("Handler Dispatch failed", err); } processdispatchresult (Processedrequest, Response, Mappedhandler, MV, dispatchexception); } Catch(Exception ex) {triggeraftercompletion (processedrequest, Response, Mappedhandler, ex); } Catch(Throwable err) {triggeraftercompletion (processedrequest, Response, Mappedhandler, NewNestedservletexception ("Handler processing failed", err)); } finally { if(asyncmanager.isconcurrenthandlingstarted ()) {//Instead of Posthandle and Aftercompletion if(Mappedhandler! =NULL) {mappedhandler.applyafterconcurrenthandlingstarted (processedrequest, response); } } Else { //used by a multipart request. if(multipartrequestparsed) {Cleanupmultipart (processedrequest); } } } }