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目录下 --------------------- 

猜你喜欢

转载自blog.csdn.net/m0_37768843/article/details/82908142
今日推荐