美文网首页
【转载】Spring加载resource时classpath*:

【转载】Spring加载resource时classpath*:

作者: wyymaomi | 来源:发表于2019-12-26 09:25 被阅读0次

在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

相关文章

网友评论

      本文标题:【转载】Spring加载resource时classpath*:

      本文链接:https://www.haomeiwen.com/subject/fmhkoctx.html