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>
-
<li class="tool-item tool-active is-like "><a href="javascript:;"><svg class="icon" aria-hidden="true"> <use xlink:href="#csdnc-thumbsup"></use> </svg><span class="name">点赞</span> <span class="count"></span> </a></li> <li class="tool-item tool-active is-collection "><a href="javascript:;" data-report-click="{"mod":"popu_824"}"><svg class="icon" aria-hidden="true"> <use xlink:href="#icon-csdnc-Collection-G"></use> </svg><span class="name">收藏</span></a></li> <li class="tool-item tool-active is-share"><a href="javascript:;"><svg class="icon" aria-hidden="true"> <use xlink:href="#icon-csdnc-fenxiang"></use> </svg>分享</a></li> <!--打赏开始--> <!--打赏结束--> <li class="tool-item tool-more"> <a> <svg t="1575545411852" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5717" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200"><defs><style type="text/css"></style></defs><path d="M179.176 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5718"></path><path d="M509.684 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5719"></path><path d="M846.175 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5720"></path></svg> </a> <ul class="more-box"> <li class="item"><a class="article-report">文章举报</a></li> </ul> </li> </ul> </div> </div> <div class="person-messagebox"> <div class="left-message"><a href="https://blog.csdn.net/wl_public"> <img src="https://profile.csdnimg.cn/A/B/B/3_wl_public" class="avatar_pic" username="wl_public"> <img src="https://g.csdnimg.cn/static/user-reg-year/2x/2.png" class="user-years"> </a></div> <div class="middle-message"> <div class="title"><span class="tit"><a href="https://blog.csdn.net/wl_public" data-report-click="{"mod":"popu_379"}" target="_blank">wl_public</a></span> </div> <div class="text"><span>发布了12 篇原创文章</span> · <span>获赞 17</span> · <span>访问量 3万+</span></div> </div> <div class="right-message"> <a href="https://im.csdn.net/im/main.html?userName=wl_public" target="_blank" class="btn btn-sm btn-red-hollow bt-button personal-letter">私信 </a> <a class="btn btn-sm bt-button personal-watch" data-report-click="{"mod":"popu_379"}">关注</a> </div> </div> </div>