1. 程式人生 > >classpath:和classpath*:的區別以及web.xml中載入多個配置檔案

classpath:和classpath*:的區別以及web.xml中載入多個配置檔案

首先我們都知道要使用spring,則需要在web.xml中增加如下程式碼: 

Xml程式碼   
 

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

spring是如何載入配置檔案肯定也跟 ContextLoaderListener類有關,該類可以作為listener 使用,它會在建立時自動查詢WEB-INF/ 下的applicationContext.xrnl 檔案。因此,如果只有一個配置檔案,並且檔名為applicationContext.xml ,則只需在web.xml加上面程式碼即可。 



    如果有多個配置檔案需要載入,則考慮使用<context-param>即元素來確定配置檔案的檔名。由於ContextLoaderListener載入時,會查詢名為contextConfigLocation的引數。因此,配置context-param時引數名字應該是contextConfigLocation。所以context-param引數的名字是固定的contextConfigLocation. 

  比如下面的示例: 



Xml程式碼  
 

<context-param>  
    <param-name>contextConfigLocation</param-name>  
    <param-value>  
        classpath*:conf/spring/applicationContext*.xml,   
        classpath*:conf/spring/applicationContext_dict*.xml
    </param-value>  
</context-param>   

 

 

公司的考勤系統程式,有5個spring配置檔案:bean-edu.xml,bean-pub.xml,db-edu.xml,db-pub.xml,timer-system.xml,均放置於src目錄下,在web.xml中配置這些檔案的程式碼如下: 

<context-param>  
        <param-name>contextConfigLocation</param-name>  
        <param-value>classpath:/db-pub.xml,  
                     classpath:db-edu.xml,  
                     classpath:bean*.xml,  
                     classpath*:timer-system.xml  
        </param-value>  
</context-param>  

注意:部署程式啟動tomcat之後,log4j顯示出 
[main] INFO  org.springframework.beans.factory.xml.XmlBeanDefinitionReader  - Loading XML bean definitions from class path resource [db-pub.xml] 
[main] INFO  org.springframework.beans.factory.xml.XmlBeanDefinitionReader  - Loading XML bean definitions from class path resource [db-edu.xml] 
[main] INFO  org.springframework.beans.factory.xml.XmlBeanDefinitionReader  - Loading XML bean definitions from file [E:\apache-tomcat-6.0.33-windows-x86\apache-tomcat-6.0.33\webapps\DigitalCampus\WEB-INF\classes\bean-edu.xml] 
[main] INFO  org.springframework.beans.factory.xml.XmlBeanDefinitionReader  - Loading XML bean definitions from file [E:\apache-tomcat-6.0.33-windows-x86\apache-tomcat-6.0.33\webapps\DigitalCampus\WEB-INF\classes\bean-pub.xml] 
[main] INFO  org.springframework.beans.factory.xml.XmlBeanDefinitionReader  -  Loading XML bean definitions from URL [file:/E:/apache-tomcat-6.0.33-windows-x86/apache-tomcat-6.0.33/webapps/DigitalCampus/WEB-INF/classes/timer-system.xml] 

 

<context-param>  
      <param-name>contextConfigLocation</param-name>  
      <param-value>classpath:/db-pub.xml,  
                     classpath:db-edu.xml,  
                     classpath*:bean*.xml,  
                     /WEB-INF/classes/timer-system.xml  
                     <!--  classpath*:timer-system.xml-->  
      </param-value>  
 </context-param>  



[main] INFO  org.springframework.beans.factory.xml.XmlBeanDefinitionReader  - Loading XML bean definitions from class path resource [db-pub.xml] 
[main] INFO  org.springframework.beans.factory.xml.XmlBeanDefinitionReader  - Loading XML bean definitions from class path resource [db-edu.xml] 
[main] INFO  org.springframework.beans.factory.xml.XmlBeanDefinitionReader  - Loading XML bean definitions from file [E:\apache-tomcat-6.0.33-windows-x86\apache-tomcat-6.0.33\webapps\DigitalCampus\WEB-INF\classes\bean-edu.xml] 
[main] INFO  org.springframework.beans.factory.xml.XmlBeanDefinitionReader  - Loading XML bean definitions from file [E:\apache-tomcat-6.0.33-windows-x86\apache-tomcat-6.0.33\webapps\DigitalCampus\WEB-INF\classes\bean-pub.xml] 
[main] INFO  org.springframework.beans.factory.xml.XmlBeanDefinitionReader  - Loading XML bean definitions from ServletContext resource [/WEB-INF/classes/timer-system.xml] 

根據以上總結
1 classpath和classpath*的區別是:前者from class path resource,後者from URL。classpath:只會到你的class路徑中查詢找檔案; 
classpath*:不僅包含class路徑,還包括jar檔案中(class路徑)進行查詢. 
2 帶不帶有/,沒有區別。 
3 bean*.xml查詢的是以bean開頭的配置檔案,from file 
4 classpath*:bean*.xml 為from file. 
5   /WEB-INF/classes/timer-system.xml 為from ServletContext resource。 



另外: 
"**/" 表示的是任意目錄; 
"**/applicationContext-*.xml" 表示任意目錄下的以"applicationContext-"開頭的XML檔案。 
程式部署到tomcat後,src目錄下的配置檔案會和class檔案一樣,自動copy到應用的 WEB-INF/classes目錄下 ---------------------