1. 程式人生 > >spring配置檔案中引用外部的properties檔案

spring配置檔案中引用外部的properties檔案

要在spring的xml中引用properties檔案中定義的屬性,需要進行特殊的載入,首先需要利用PropertyPlaceholderConfigurer

典型配置如下:
<bean id="common.propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreResourceNotFound" value="true" />
<property name="ignoreUnresolvablePlaceholders" value="true" />
<property name="fileEncoding" value="UTF-8" />
<property name="locations">
<list>
<value>classpath*:conf/default-*.properties</value>
<value>classpath*:conf/*.properties</value>
</list>
</property>
</bean>

這個是呼叫的locations的list賦值方式,也可以單獨一條
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" value="classpath:com/foo/jdbc.properties"/>
</bean>

properties中配置:
jdbc.driverClassName=org.hsqldb.jdbcDriver

下面的內容中,就可以通過
value="${jdbc.driverClassName}"
來訪問properties的值