【Spring】Web容器初始化过程(十七)

一、初始化过程图

  

上图展示了web容器初始化的过程,其官方文档给出了这样的描述:

  When a web application is deployed into a container, the following steps must be performed, in this order, before the web application begins processing client requests.

  1. Instantiate an instance of each event listener identified by a <listener> element in the deployment descriptor.
  2. For instantiated listener instances that implement ServletContextListener, call the contextInitialized() method.
  3. Instantiate an instance of each filter identified by a <filter> element in the deployment descriptor and call each filter instance's init() method.
  4. Instantiate an instance of each servlet identified by a <servlet> element that includes a <load-on-startup> element in the order defined by the load-on-startup element values, and call each servlet instance's init() method.

二、web.xml的配置  

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3     xmlns="http://java.sun.com/xml/ns/javaee"
 4     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
 5     id="WebApp_ID" version="3.0">
 6     <display-name>test-springmvc</display-name>
 7 
 8     <!-- 指定Spring Bean的配置文件所在目录。默认配置在WEB-INF目录下 -->
 9     <context-param>
10         <param-name>contextConfigLocation</param-name>
11         <param-value>classpath:spring/spring-context.xml</param-value>
12     </context-param>
13 
14     <!-- Spring监听器 -->
15     <listener>
16         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
17     </listener>
18 
19     <!-- 中文过滤器 -->
20     <filter>
21         <filter-name>CharacterEncodingFilter</filter-name>
22         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
23         <init-param>
24             <param-name>encoding</param-name>
25             <param-value>UTF-8</param-value>
26         </init-param>
27         <init-param>
28             <param-name>forceEncoding</param-name>
29             <param-value>true</param-value>
30         </init-param>
31     </filter>
32     <filter-mapping>
33         <filter-name>CharacterEncodingFilter</filter-name>
34         <url-pattern>/*</url-pattern>
35     </filter-mapping>
36 
37     <!-- Spring MVC配置 -->
38     <servlet>
39         <servlet-name>spring</servlet-name>
40         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
41         <!-- 可以自定义servlet.xml配置文件的位置和名称,默认为WEB-INF目录下,名称为[<servlet-name>]-servlet.xml,如spring-servlet.xml -->
42         <init-param>
43             <param-name>contextConfigLocation</param-name>
44             <param-value>classpath:spring/spring-mvc.xml</param-value>
45         </init-param>
46         <load-on-startup>1</load-on-startup>
47     </servlet>
48 
49     <servlet-mapping>
50         <servlet-name>spring</servlet-name>
51         <url-pattern>/</url-pattern>
52     </servlet-mapping>
53 
54 
55 
56     <welcome-file-list>
57         <welcome-file>index.jsp</welcome-file>
58     </welcome-file-list>
59 </web-app>  

  上是截取的web.xml中的配置,在<listener>标签中定义了spring容器加载监听器;在<filter>标签中定义了过滤器;在<servlet>标签中定义了spring前端控制器。

三、启动源码分析

  可以在Web项目启动过程中,进行调试,得到以下启动步骤

  

  由上可知初始化顺序:监听器 -> 过滤器 -> Servlet

 参考:https://www.cnblogs.com/RunForLove/p/5688731.html

原文地址:https://www.cnblogs.com/h--d/p/14725115.html