SpringMVC 源码之容器配置及启动

Java Web项目中的一个配置文件

web.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<web-app>
<!-- Spring 配置 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<!-- org.springframework.web.context.ContextLoader#initWebApplicationContext -->
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-container.xml</param-value>
</context-param>

<!-- SpringMVC 配置-->
<servlet>
<servlet-name>app</servlet-name>
<!-- org.springframework.web.servlet.DispatcherServlet#init -->
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc-container.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>app</servlet-name>
<url-pattern>/app/*</url-pattern>
</servlet-mapping>
</web-app>

启动顺序

通过注册 Web 服务器的监听器 ContextLoaderListener, 加载 Spring 配置文件并启动;
在加载Servlet时, DispatcherServlet 引导 SpringMVC 启动;
通过启动 SpringMVC 事件加载内置组件(可覆盖 DispatcherServlet.properties 自定义配置)。

SpringBoot 中的顺序是不一样的, 是由Spring内嵌 使用 Spring 配置来引导自身和嵌入式 Servlet 容器。 Filter 和 Servlet 声明在 Spring 配置中检测到并在 Servlet 容器中注册。有关更多详细信息参阅 Spring 文档

链接

DispatcherServlet