PropertyPlaceholderConfigurer
PropertyPlaceholderConfigurer用于在配置Bean时引用外部属性文件。
方法一:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" value="classpath:com/foo/jdbc.properties"/>
</bean>
<bean id="dataSource" destroy-method="close"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
方法二:
<context:property-placeholder location="classpath:com/foo/jdbc.properties"/>
属性文件引用自身
dbName=sampledb
driverClassName=com.mysql.jdbc.Driver
uri=jdbc:mysql://localhost:3306/${dbName}
注:如果一个属性值太长,一行写不下,可以通过在行后添加“\”将属性值划分为多行
desc=desc context desc context \
desc context