zoukankan      html  css  js  c++  java
  • 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>

     

  • 相关阅读:
    PAT (Advanced Level) 1114. Family Property (25)
    PAT (Advanced Level) 1113. Integer Set Partition (25)
    PAT (Advanced Level) 1112. Stucked Keyboard (20)
    PAT (Advanced Level) 1111. Online Map (30)
    PAT (Advanced Level) 1110. Complete Binary Tree (25)
    PAT (Advanced Level) 1109. Group Photo (25)
    PAT (Advanced Level) 1108. Finding Average (20)
    PAT (Advanced Level) 1107. Social Clusters (30)
    PAT (Advanced Level) 1106. Lowest Price in Supply Chain (25)
    PAT (Advanced Level) 1105. Spiral Matrix (25)
  • 原文地址:https://www.cnblogs.com/itmyhome/p/4131612.html
Copyright © 2011-2022 走看看