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

相關文章
相關標籤/搜索