spring classpath

<p>在spring配置文件里,能够用classpath:前缀,来从classpath中加载资源</p> <p>好比在src下有一个jdbc.properties的文件,能够用以下方法加载: </p> <p>&lt;bean id=&quot;propertyConfigurer&quot; <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; class=&quot;org.springframework.beans.factory.config.PropertyPlaceholderConfigurer&quot;&gt; <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;property name=&quot;locations&quot;&gt; <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;list&gt; <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;value&gt;classpath:jdbc.properties&lt;/value&gt; <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;/list&gt; <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;/property&gt; <br />&#160;&#160;&#160;&#160; &lt;/bean&gt;</p> <p>对于打包在jar包中的资源,也能够用一样的方式:</p> <p>&lt;import resource=&quot;classpath:META-INF/cxf/cxf.xml&quot; /&gt;</p> <p>另一种很像的方式,是使用classpath*:前缀,好比 </p> <p>&lt;property name=&quot;mappingLocations&quot;&gt; <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;list&gt; <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;value&gt;classpath*:/hibernate/*.hbm.xml&lt;/value&gt; <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;/list&gt; <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;/property&gt;</p> <p>classpath:与classpath*:的区别在于,前者只会从第一个classpath中加载,然后者会从全部的classpath中加载&#160; <br />若是要加载的资源,不在当前ClassLoader的路径里,那么用classpath:前缀是找不到的,这种状况下就须要使用classpath*:前缀&#160; <br />另外一种状况下,在多个classpath中存在同名资源,都须要加载,那么用classpath:只会加载第一个,这种状况下也须要用classpath*:前缀&#160; <br />可想而知,用classpath*:须要遍历全部的classpath,因此加载速度是很慢的,所以,在规划的时候,应该尽量规划好资源文件所在的路径,尽可能避免使用classpath*</p>spring

相关文章
相关标签/搜索