• web.xml加载过程


    web.xml加载过程:
    1 启动WEB项目的时候,容器(如:Tomcat)会读他的配置文件web.xml读两个节点
      <listener></listener>和<context-param></context-param>
    2 紧接着,容器创建一个ServletContext(上下文) 这个WEB项目所有部分都将共享这个上下文
    3 容器将<context-param></context-param>转化为键值对并交给ServletContext
    4 容器创建<listener></listener>中的类的实例,即创建监听
    5 在监听中会有contextInitialized(ServletContextEvent args)初始化方法,在
    这个方法中获得:
      ServletContext = ServletContextEvent.getServletContext();  
      context-param的值 = ServletContext.getInitParameter("context-param的键"); 

    web.xml节点加载顺序
      节点的加载顺序与它们在web.xml文件中的先后顺序无关。即不会因为filter写在listener的前面而会先加载filter最终得出的结论是:listener->filter->servlet
      同时还存在着这样一种配置节点:context-param,它用于向 ServletContext 提供键值对,即应用程序上下文信息。我们的 listener, filter 等在初始化时会用到这些上下文中的信息,那么 context-param 配置节是不是应该写在 listener 配置节前呢?实际上 context-param 配置节可写在任意位置,因此真正的加载顺序为:
    context-param -> listener -> filter -> servlet
    加载spring
    <listener> 
            <listener-class> 
                 org.springframework.web.context.ContextLoaderListener  
            </listener-class> 
    </listener>
    最终结论:

    web.xml 的加载顺序是:[context-param -> listener -> filter -> servlet -> spring] ,而同类型节点之间的实际程序调用的时候的顺序是根据对应的 mapping 的顺序进行调用的。
     
    <!--上下文参数用于log4j以及spring中使用-->
    <context-param>
        <param-name>webAppRootKey</param-name>
        <param-value>/WEB-INF/log4j.properties</param-value>
    </context-param>

    <!--应用程序上下文参数,指定spring配置文件位置-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/beans.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener>

    <!--监听器 用于初始化spring框架-->
    <listener>
         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

  • 相关阅读:
    手机储存卡被写保护怎样删掉
    EXT.NET复杂布局(四)——系统首页设计(下)
    排序算法——选择排序
    NET 程序员十种必备工具 目录
    三种序列化体式格式
    Limitations of SharePoint Web Services
    How to: Update List Items
    哄骗JQuery直接调用asp.net后台办法
    jquery Dialog 弹出窗插件
    EXT.NET高效开发(四)——实用技巧
  • 原文地址:https://www.cnblogs.com/itmyhome/p/3315912.html
Copyright © 2020-2023  润新知