These two weeks are looking at the Spring MVC source code, it seems to be still a lot of details do not understand clearly, here to read the record, welcome to discuss together in the comments.
First, Bedding
Spring MVC is based on Servlets, and before formal analysis, let's look at the knowledge of the servlet. The life cycle of the servlet is built by three methods Init, service, Destory.
The init () method is executed only once in the life cycle of the servlet. It is executed when the server loads the servlet and is responsible for initializing the Servlet object. You can configure the server to get the servlet into the servlet at the start of the server or client for the first time. Init () is not repeated regardless of how many clients access the servlet.
It is the core of the servlet and is responsible for responding to customer requests. Whenever a client requests a HttpServlet object, the service () method of the object is called and passed to the method a "request" (ServletRequest) object and a "response" (Servletresponse) object as a parameter. The service () method already exists in HttpServlet. The default service function is to invoke the DO function corresponding to the method of the HTTP request.
Executes only once, and executes the method when the server-side stops and the servlet is unloaded. When a Servlet object exits its lifecycle, it is responsible for releasing the resource that is occupied. A servlet may produce additional threads when running the service () method, so it is necessary to confirm that the threads have been terminated or completed when the Destroy () method is called.
II. Spring MVC startup process
the entrance to spring MCV is Dispatcherservlet, as the name implies is the dispatch servlet is the service request scheduling, its inheritance structure is as follows:
When integrating spring MVC, the Web. XML has this configuration, and I'm sure we know
<servlet> <servlet-name>mvc-dispatcher</servlet-name> <servlet-class> org.springframework.web.servlet.dispatcherservlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:mvc-dispatcher.xml </param-value> </init-param> <load-on-startup>1</load-on-startup> </ servlet> <servlet-mapping> <servlet-name>mvc-dispatcher</servlet-name> <url-pattern>*. do </url-pattern> </servlet-mapping>
That is, based on the knowledge of the servlet, when the server is started, the Dispatcherservlet object is created, the Init method is executed, and the Init method is found in Httpservletbean based on the inheritance of Dispaterservlet. Now let's take a look at this.
Method
Public Final voidInit ()throwsservletexception {if(logger.isdebugenabled ()) {Logger.debug ("Initializing servlet" + getservletname () + "'"); } //Set Bean properties from init parameters. Try { /*Propertyvaluesz is encapsulated in Web. XML Config servlet parameter information * <init-param> <PARAM-NAME>CONTEXTCONFI Glocation</param-name> <param-value>classpath:mvc-dispatcher.xml</param-value> </init-param>*/propertyvalues PVs=NewServletconfigpropertyvalues (Getservletconfig (), This. requiredproperties); Beanwrapper BW= Propertyaccessorfactory.forbeanpropertyaccess ( This); Resourceloader Resourceloader=NewServletcontextresourceloader (Getservletcontext ()); Bw.registercustomeditor (Resource.class,NewResourceeditor (Resourceloader, Getenvironment ())); Initbeanwrapper (BW); //set the configured initialization value to DispatcherservletBw.setpropertyvalues (PVS,true); } Catch(Beansexception ex) {Logger.error ("Failed to set beans properties on servlet '" + getservletname () + "'", ex); Throwex; } //initializes the Spring MVC container Method Webapplicationcontext, which is implemented by the Frameworkservlet of subclassesInitservletbean (); if(logger.isdebugenabled ()) {Logger.debug ("Servlet" + getservletname () + "' configured successfully"); } }
Next look at Frameworkservlet's Initservletbean (), the main tune Initwebapplicationcontext () method
protected Final voidInitservletbean ()throwsservletexception {getservletcontext (). log ("Initializing Spring frameworkservlet '" + getservletname () + "'"); if( This. logger.isinfoenabled ()) { This. Logger.info ("Frameworkservlet" + getservletname () + "': Initialization Started"); } LongStartTime =System.currenttimemillis (); Try { //initializing the Spring MCV container This. Webapplicationcontext =Initwebapplicationcontext (); Initframeworkservlet (); } Catch(Servletexception ex) { This. Logger.error ("Context initialization Failed", ex); Throwex; } Catch(RuntimeException ex) { This. Logger.error ("Context initialization Failed", ex); Throwex; } if( This. logger.isinfoenabled ()) { LongElapsedTime = System.currenttimemillis ()-StartTime; This. Logger.info ("Frameworkservlet" + getservletname () + "': Initialization completed in" +ElapsedTime+ "MS"); } }
Initwebapplicationcontext method
protectedWebapplicationcontext Initwebapplicationcontext () {/*During spring startup, Contextloaderlistener listens to, instantiates, and registers the IOC container with the ServletContext, and now takes out the IOC container .*/Webapplicationcontext Rootcontext=Webapplicationcontextutils.getwebapplicationcontext (Getservletcontext ()); Webapplicationcontext WAC=NULL; if(WAC = =NULL) { //send the IOC container in to create spring MVC's own containerWAC =Createwebapplicationcontext (Rootcontext); } returnWAC; if( This. Publishcontext) {String Attrname=Getservletcontextattributename (); //set the created spring MCV's own container to the ServletContext containerGetservletcontext (). SetAttribute (Attrname, WAC); } }
Createwebapplicationcontext method
protectedWebapplicationcontext Createwebapplicationcontext (applicationcontext parent) {/*decide which webapplicationcontext to use based on the configuration in Web. Xml. By default, the name "Contextclass" for the associated configuration Context-param in Xmlwebapplicationcontext Web. XML is used*/Class<?> Contextclass =Getcontextclass (); //get spring MVC own containerConfigurablewebapplicationcontext WAC =(Configurablewebapplicationcontext) Beanutils.instantiateclass (Contextclass); Wac.setenvironment (Getenvironment ()); //specifies that the parent container is an IOC containerwac.setparent (parent); //Specify the location of the spring MCV core configuration file Classpath:mvc-dispatcher.xmlwac.setconfiglocation (Getcontextconfiglocation ()); //Spring MCV Container continues to initializeConfigureandrefreshwebapplicationcontext (WAC); returnWAC; }
Source code Analysis of Spring MVC initiation process