During the launch of a Web project, we want to know what the general process is, so that we can add functionality to each process or help us out of the wrong line.
We know that when we start the Tomcat container, the container first initializes some of the necessary components, loads the jar packages referenced by the project (from the Jdk,tomcat, and the Lib directory in the Web-inf, respectively). Then the next step is to read the Web project's XML configuration file. Therefore, the Web project must have an XML configuration file.
Let's take a look at the standard Web. XML configuration file, which I extracted from my project.
<?xml version= "1.0" encoding= "UTF-8"?>
<web-app xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
xmlns= "Http://java.sun.com/xml/ns/javaee"
xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_1.xsd"
version= "3.1" >
<!--Spring context --
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
Classpath:ApplicationContext.xml,
</param-value>
</context-param>
<!--load log4j configuration file --
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.properties</param-value>
</context-param>
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>www.warrior.com</param-value>
</context-param>
<!--listener --
<listener>
<listener-class>org.springframework.web.util.log4jconfiglistener
</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.contextloaderlistener
</listener-class>
</listener>
<!--character encoding filters --
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.characterencodingfilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--Initialize Filter --
<filter>
<filter-name>startFilter</filter-name>
<filter-class>com.xdx.filter.StartFilter</filter-class>
</filter>
<!--session Filter --
<filter>
<filter-name>springSessionRepositoryFilter</filter-name>
<filter-class>org.springframework.web.filter.delegatingfilterproxy
</filter-class>
</filter>
<filter-mapping>
<filter-name>springSessionRepositoryFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--The following configuration is Spring MVC --
<servlet>
<servlet-name>springMvc</servlet-name>
<servlet-class>org.springframework.web.servlet.dispatcherservlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:ApplicationContext-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<error-page>
<error-code>404</error-code>
<location>/404.jsp</location>
</error-page>
<session-config>
<session-timeout>600</session-timeout>
</session-config>
</web-app>
You can see that there are several main configuration items for Web. Xml.
A.context-param: Context parameters, configure some contextual information by key-value pairs, such as contextconfiglocation, we configure him as Classpath: Applicationcontext.xml, to classpath:ApplicationContext.xml this place to find Spring's master profile.
B.listener:listener is the listener that listens to changes (such as the initialization of the servlet) and then triggers the listener's code.
C.filter: Filter, as the name implies, is to filter the request, filter will also be instantiated when the project starts. A filter is typically used to correspond to filter-mapping, which filters the URL path to the code in the filter to be executed. If a filter does not have a filter-mapping, it does not have the meaning of existence, its purpose in the Web. XML is purely to be instantiated at the time of project startup, thus executing its internal code. The Startfilter in the above configuration file is the function.
The configuration of D.servlet,servlet is similar to filter, which is to intercept requests, and different requests are assigned to different servlet classes for processing.
In order to observe the start order of each component in the project, I added the static code block and the parameterless constructor to the relevant Dao,entity class, service class, and Controllers class. The static code block is run when the class is loaded, and the constructor runs when the class is instantiated, as shown below.
Run the project. Look at the logs printed by the console.
As we can see, the start order of the project is Context-param first, then listener, and then the filter, and finally the servlet.
PS1: Change the order of the Context-param,listener,filter,servlet configuration statement in Web. XML, will its boot order be changed?
The answer is no, even if the configuration of the servlet is placed first, the order of loading will be the last. It is important to note, however, that in the same type of configuration item, the order of the Web. XML is affected by the order in which it is started, for example, if there are two filter,filter1 in the configuration file prior to Filter2, Filter1 is loaded before Filter2 is instantiated.
The role of Ps2:org.springframework.web.context.ContextLoaderListener.
Contextloade Rlistener This listener inherits from Contextloader and implements Servletcontextlistener, His main role is to find and read the Spring Master Profile Applicationcontext.xml (that is, the contextconfiglocation defined in Context-param), and then start Webapplicationcontext, which can also be called Web App context, and most importantly, it will webapplicationcontext Injected into the ServletContext container (as servletcontext of a attribute, property), and in webapplicationcontext A ServletContext reference is retained in the . So we can pass
webapplicationcontext get ServletContext, can also get to webapplicationcontext through ServletContext .
Get ServletContext by Webapplicationcontext:
Webapplicationcontext Webapplicationcontext = Contextloader.getcurrentwebapplicationcontext ();
ServletContext ServletContext = Webapplicationcontext.getservletcontext ();
get through ServletContext Webapplicationcontext:
ServletContext ServletContext = Event.getservletcontext ();
ApplicationContext application = webapplicationcontextutils. Getwebapplicationcontext (servletContext);
Ps3:webapplicationcontext and ServletContext who existed first?
Of course, Servletcontext,servletcontext is a Web container (tomcat, etc.) that provides a global context for a Web project, with only one Web project. It is actually a host of the Webapplicationcontext container that is generated later.
So: To put it simply: Web project startup follows these steps.
1. Start the project and load the dependent jar package.
The 2.web container (Tomcat) first provides a global context ServletContext.
3. The Web container goes to read the. xml file and runs the Contextloaderlistener listener, which implements the Servletcontextlistener interface so that when a ServletContext instance is found, the container executes serv The initialization method of the Letcontextlistener interface, in which the main configuration file of spring is read according to the location specified in the Contextconfiglocation, The Web App context Webapplicationcontext is then generated and injected as a property into the ServletContext.
4. After initializing Webapplicationcontext, the "Business layer" Spring container is started and the classes scanned in the disease initialization ApplicationContext configuration file are started loading.
5. Then initialize the filter, and finally initialize the servlet.
so, as a Web project, Webapplicationcontext The build must be implemented in the presence of the Web container, because he needs servletcontext, and ServletContext is generated by the Web container.
What is Ps4:dispatcherservlet? What is the use of.
In short, it's a servlet, but it's a special servlet, the core of the entire Spring MVC framework, a front-end servlet,spring MVC that passes through the front-end servlet to accept all requests, Then the specific work to be distributed to other servlets to implement the specific.
At the same time, in the configuration file of the servlet, we see the configuration file named Springmvc read the contextconfiglocation definition (classpath: Applicationcontext-mvc.xml), launches the spring container for the web layer, in which we initialize all the controller classes. As shown in the log that the console prints.
PS5: Since the initialization of Dispatcherservlet is accompanied by the launch of the Spring MVC container (that is, the Web layer container above), it takes a long time, so we want to initialize the project when it starts. That's why we set the Load-on-startup item to 1. Because this property is set to a positive number, the representation is initialized at the start of the project, and smaller numbers indicate that the earlier initialization is possible. If we set it to a negative number. The Spring MVC container will not start when the project is started, but it will load the boot container for the first time when we visit the action of a controller, which would cause long waits, so we will generally Load-on-startup is set to 1.
Web Project Initiation Process Exploration