常规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>servletjava

相关文章
相关标签/搜索