SPRINGMVC servlet Configuration and startup
Take a look at the SPRINGMVC Web. XML Common configuration:
<Servlet><!--configuration Dispatcherservlet--<Servlet-name>springmvc</Servlet-name><Servlet-class>org.springframework.web.servlet.dispatcherservlet</Servlet-class><!--specify that the spring MVC profile location is not specified using the default--<Init-param><Param-name>contextconfiglocation</Param-name><Param-value>classpath:spring/spring-mvc.xml</Param-value> </init-param> <!--Set boot order-- > <load-on-startup>1</load-on-startup> </servlet> <!--ServLet matching Map--< servlet-mapping> <servlet-name>springmvc</servlet-name> < Url-pattern>/</url-pattern> </SERVLET-MAPPING>
Dispatcherservlet is a special servlet, so this article first looks at the features of the regular servlet.
1. servlet feature 1.1 servlet startup process
What is the startup process of the servlet? Does it start with the SPRINGMVC IOC container as it launches with the Web container?
In fact, think carefully, you can guess that the servlet is not necessarily created with the Web container launch, we know that a Web container may have a lot of servlets, if there is a Baichi servlet, the server starts to instantiate so many classes, Obviously, the amount of load is quite large for memory. In fact, in the <servlet> tag of Web. XML, you can <load-on-startup> control whether the servlet is initialized with the start of the website container and the initialization sequence when multiple servlets are created at the same time.
Loadonstartup settings are divided into three scenarios:
Loadonstartup < 0 when the Web container is started without instantiation, the servlet is instantiated the first time it is invoked, LOADONSTARTUPM by default (that is, when there is no configuration).
Loadonstartup > 0 when the Web container is started, the order is instantiated from small to large, and the positive integers are small.
Loadonstartup = 0 when the Web container is started, it is instantiated, which is equivalent to the largest integer, so when the Web container starts, it is finally instantiated.
Additionally, the <servlet-mapping> request is intercepted and the corresponding servlet processes the request. The above configuration <url-pattern>/</url-pattern> means that all url,dispatchservlet except JSPs will be processed by these requests.
1.2 Servlet life cycle
- The Servlet initializes by calling the Init () method.
- The Servlet invokes the service () method to handle the client's request.
- The Servlet terminates (ends) by calling the Destroy () method.
- Finally, the Servlet is garbage collected by the JVM's garbage collector.
Demonstrates a typical servlet life cycle scenario:
The servlet's init () method is called by the container when the servlet is initialized, and the INI () method of the servlet is called when the ServletConfig object is passed to the servlet, and when the user makes an HTTP request, is delegated to the Web container (servlet container), when multiple requests are initiated at the same time by the servlet container thread pool, where multiple thread-processing tasks are allocated, and each thread executes a single Servlet instance of the service () method.
2. Dispatcherservlet
Dispatcherservlet is also essentially a servlet, inherited from an inheritance relationship, which inherits the Init () method implemented in Httpservletbean,httpservletbean because the Init method that will invoke it when the Web container starts , the effect of this method:
- Sets the servlet initialization parameter (Init-param) to the component, and the primary ServletConfig object is passed to the servlet.
- Provided to subclass initialization extension point, Initservletbean ()
2.1 Servlet Load configuration file
Slightly
2.1 Initializing Servletbean
Frameworkservlet inherits the Httpservletbean, which initializes the web context through Initservletbean ().
@Overrideprotected final void Initservletbean () throws Servletexception { try { //1. Initialize the web context This.webapplicationcontext = Initwebapplicationcontext (); 2. Extension Point Initframeworkservlet () provided to subclass initialization ; } Catch ... }}
Initwebapplicationcontext Initialize Webapplicationcontext:
Protected Webapplicationcontext Initwebapplicationcontext () {//Find the parent container, which is the IOC container that triggers execution when the Web container loads Contextloaderlistener Webapplicationcontext Rootcontext = Webapplicationcontextutils.getwebapplicationcontext (This.getServletContext ()) ; Webapplicationcontext WAC = null; if (this.webapplicationcontext! = null) {//1. Create Dispatherservlet own container WAC = This.webapplicationcontext ; if (WAC instanceof Configurablewebapplicationcontext) {Configurablewebapplicationcontext AttrName = (Config Urablewebapplicationcontext) WAC; if (!attrname.isactive ()) {if (attrname.getparent () = = null) {//1.1 specifies the parent container, which is cont Extloaderlistener process loaded IOC container attrname.setparent (rootcontext); }//1.2 executes Wac.refresh (), which is the container initialization this.configureandrefreshwebapplicationcontext (attr Name); }}} if(WAC = = null) {//2. Find the already bound context WAC = This.findwebapplicationcontext (); if (WAC = = null) {//3. If the corresponding context is not found and the father is contextloaderlistener WAC = This.createwebapplica Tioncontext (Rootcontext); } if (!this.refresheventreceived) {///4. Refresh the context (perform some initialization) This.onrefresh (WAC); } if (This.publishcontext) {String attrName1 = This.getservletcontextattributename (); This.getservletcontext (). SetAttribute (attrName1, WAC); if (this.logger.isDebugEnabled ()) {This.logger.debug ("Published webapplicationcontext of servlet \ '" + This . Getservletname () + "\ ' as ServletContext attribute with name [" + AttrName1 + "]"); }} return WAC; }
From the above see Dispatcherservlet will maintain a own webapplicationcontext,contextloaderlistener in the creation of ApplicationContext as Rootcontext, Pass in the Dispatcherservlet container, and then initialize your own IOC container.
The difference between the two containers:
- The Webapplicationcontext created by Contextloaderlistener is shared across the Web application.
- The ApplicationContext created by Dispatcherservlet is used primarily for components related to the servlet, such as Controller, Viewresovler, and so on.
In another point, the procedure in the preceding code this.onRefresh(wac) triggers the Dispatchservlet initial session method Onrefresh (WAC):
protected void Onrefresh (ApplicationContext context) { this.initstrategies (context);} protected void Initstrategies (ApplicationContext context) { this.initmultipartresolver (context); This.initlocaleresolver (context); This.initthemeresolver (context); This.inithandlermappings (context); This.inithandleradapters (context); This.inithandlerexceptionresolvers (context); This.initrequesttoviewnametranslator (context); This.initviewresolvers (context); This.initflashmapmanager (context);}
This process is the nine main strategies used by the Spring WEB MVC framework to initialize the default, and these nine strategies refer to the next article.
SPRINGMVC Web. XML Configuration--Dispatcherservlet