web.xml中配置的加载优先级context-param -> listener -> filter -> servlet
1.context-param,它用于向 ServletContext 提供键值对,即应用程序上下文信息。我们的 listener, filter 等在初始化时会用到这些上下文中的信息所以context-param最先加载。
2.Listener加载
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
Spring提供ServletContentListener的一个实现类ContextLoaderListener监听器,该类可以作为Listener使用,在启动Tomcat
容器的时候,该类的作用就是自动装载ApplicationContext的配置信息,如果没有设置contextConfigLocation的初始参数则会
使用默认参数WEB-INF路径下的application.xml文件。如果需要自定义读取多个配置文件或者修改默认路径,则可以在web.xml
中设置:
3. filter加载
4.servlet加载
<!--前端控制器-->
<servlet>
<!--这里的名字应和前端容器的配置文件的名字一样,后面的“-servlet.xml”编译器自动补齐-->
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
web.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1"
metadata-complete="true">
<!--监听器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--根容器/上下文容器-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<!--前端控制器-->
<servlet>
<!--这里的名字应和前端容器的配置文件的名字一样,后面的“-servlet.xml”编译器自动补齐-->
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>