常规web.xml加载过程详解

show me code

<?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_0.xsd"
         version="3.0">-->
<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_0.xsd"
         version="3.1">
    <!--部署到容器时的描述性名字-->
    <display-name>spring</display-name>
    <!--部署到容器时描述性文字-->
    <description>spring example</description>
    <!--该元素用来声明应用范围(整个WEB项目)内的上下文初始化参数-->
    <!-- Spring和mybatis的配置文件 -->
    <context-param>
        <!--这里的param-name这个名字,是容器默认的,如果不指定那么容器会去默认查找获取WEB-INF下面的application.xml文件-->
        <!--配置了这个参数才可以和web页面相通-->
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring-mybatis.xml</param-value>
    </context-param>
    <!-- 编码过滤器 -->
    <filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <async-supported>true</async-supported>
        <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>
    <!-- Spring监听器 -->
    <!--实现的是ServletContextListener对象监听器,也就是它能够监听ServletContext对象的生命周期,实际上就是监听 Web 应用的生命周期-->
    <!--项目启动时执行contextInitialized方法,在spring中就是将bean装进ioc容器;-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!-- 防止Spring内存溢出监听器 -->
    <listener>
        <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
    </listener>

    <!-- Spring MVC servlet -->
    <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <!--这里的param-name这个名字,是容器默认的,如果不指定那么容器会去默认查找获取WEB-INF下面的spring-servlet.xml文件-->
            <!--这是spring的servlet,因此也是将bean装进ioc容器-->
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-mvc.xml</param-value>
        </init-param>
        <!--如果有多个servlet,这是启动顺序-->
        <load-on-startup>1</load-on-startup>
        <async-supported>true</async-supported>
    </servlet>
    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <!--以”/’开头只能匹配到路径映射如/login,和以”/*”结尾的可以做路径映射和扩展映射如*.htm-->
        <!--以前缀”*.”开头的是用来做扩展映射的-->
        <!--“/” 是用来定义default servlet映射的-->
        <!--剩下的都是用来定义详细映射的。比如: /aa/bb/cc.action-->
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <!--输入ip或者域名+端口+项目访问路径,所发出的默认请求-->
    <welcome-file-list>
        <welcome-file>/index.jsp</welcome-file>
    </welcome-file-list>

</web-app>

总结

当你根据源码点击去的时候比如org.springframework.web.context.ContextLoaderListener,启动调试,你会发现他的加载顺序是context-param>listener>filter>servlet

转载于:https://my.oschina.net/u/2609727/blog/776113

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值