web.xml啓動順序

1、概述web

一、啓動一個WEB項目的時候,WEB容器會去讀取它的配置文件web.xml,讀取<listener>和<context-param>兩個結點。 spring

二、緊急着,容建立一個ServletContext(servlet上下文),這個web項目的全部部分都將共享這個上下文。 app

三、容器將<context-param>轉換爲鍵值對,並交給servletContext。 post

1  <context-param>
2     <param-name>contextConfigLocation</param-name>
3     <param-value>classpath:context/root-context.xml</param-value>
4   </context-param>

 

四、容器建立<listener>中的類實例,建立監聽器。性能

1 <listener>
2     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
3   </listener>

 

 

加載順序url

首先能夠確定的是,加載順序與它們在 web.xml 文件中的前後順序無關。即不會由於 filter 寫在 listener 的前面而會先加載 filter。最終得出的結論是:ServletContext -> listener -> filter -> servletspa

        同時還存在着這樣一種配置節:context-param,它用於向 ServletContext 提供鍵值對,即應用程序上下文信息。咱們的 listener, filter 等在初始化時會用到這些上下文中的信息,那麼 context-param 配置節是否是應該寫在 listener 配置節前呢?實際上 context-param 配置節可寫在任意位置,所以真正的加載順序爲:context-param -> listener -> filter -> servletcode

 

 

 

 

 

 

 

 

首先 classpath是指 WEB-INF文件夾下的classes目錄 

解釋classes含義: 
1.存放各類資源配置文件 eg.init.properties log4j.properties struts.xml 
2.存放模板文件 eg.actionerror.ftl 
3.存放class文件 對應的是項目開發時的src目錄編譯文件 
總結:這是一個定位資源的入口 

若是你知道開發過程當中有這麼一句話:慣例大於配置 那麼也許你會改變你的想法 

對於第二個問題 
這個涉及的是lib和classes下文件訪問優先級的問題: lib>classes 
對於性能的影響應該不在這個範疇 

classpath 和 classpath* 區別: 
classpath:只會到你的class路徑中查找找文件; 
classpath*:不只包含class路徑,還包括jar文件中(class路徑)進行查找.
orm

 

 1   <context-param>
 2     <param-name>contextConfigLocation</param-name>
 3     <param-value>classpath:context/root-context.xml</param-value>
 4   </context-param>
 5   <servlet>
 6     <servlet-name>springMvc</servlet-name>
 7     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
 8     <init-param>
 9       <param-name>contextConfigLocation</param-name>
10       <param-value>classpath*:servlet-context.xml</param-value>
11     </init-param>
12     <load-on-startup>1</load-on-startup>
13   </servlet>
14   <servlet-mapping>
15     <servlet-name>springMvc</servlet-name>
16     <url-pattern>/</url-pattern>
17   </servlet-mapping>
相關文章
相關標籤/搜索