【转载】Spring加载resource时classpath*:与classpath:的区别



免责声明:
    本文转自网络文章,转载此文章仅为个人收藏,分享知识,如有侵权,请联系博主进行删除。
    原文作者:kyfxbl
    原文地址: spring配置中classpath和classpath*的区别
 

在spring配置文件里,可以用classpath:前缀,来从classpath中加载资源 

比如在src下有一个jdbc.properties的文件,可以用如下方法加载:

<bean id="propertyConfigurer"  
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
        <property name="locations">  
            <list>  
                <value>classpath:jdbc.properties</value>  
            </list>  
        </property>  
    </bean>

对于打包在jar包中的资源,也可以用同样的方式:

<import resource="classpath:META-INF/cxf/cxf.xml" />

另外一种很像的方式,是使用classpath*:前缀,比如

<property name="mappingLocations">  
            <list>  
                <value>classpath*:/hibernate/*.hbm.xml</value>  
            </list>  
        </property>

classpath:与classpath*:的区别在于,前者只会从第一个classpath中加载,而后者会从所有的classpath中加载。

在多个classpath中存在同名资源,都需要加载,那么用classpath:只会加载第一个,这种情况下也需要用classpath*:前缀可以加载所有的资源。


可想而知,用classpath*:需要遍历所有的classpath,所以加载速度是很慢的,因此,在规划的时候,应该尽可能规划好资源文件所在的路径,尽量避免使用classpath*

原文地址:https://www.cnblogs.com/sdjnzqr/p/3819736.html