applicationContext.xml配置
<!-- 定义数据源Bean,使用DBCP数据源实现 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${dbcp.driverClassName}" />
<property name="url" value="${dbcp.url}" />
<property name="username" value="${dbcp.username}" />
<property name="password" value="${dbcp.password}" />
<property name="maxActive" value="30"/>
<property name="maxIdle" value="${dbcp.maxIdle}" />
<property name="maxWait" value="${dbcp.maxWait}" />
<property name="defaultAutoCommit" value="${dbcp.defaultAutoCommit}"/>
<property name="removeAbandoned" value="${dbcp.removeAbandoned}"/>
<property name="removeAbandonedTimeout" value="${dbcp.removeAbandonedTimeout}"/>
<property name="logAbandoned" value="${dbcp.logAbandoned}"/>
</bean>以${}方式读取properties
来看一个Bean定义档的实际例子:
beans-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="configBean" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>jdbc/${param}/hello.properties</value>
</list>
</property>
<property name="propertiesPersister">
......
</property>
</bean>
</beans>
另外可以通过propertiesPersister对数据库密码加解密
实际上,PropertyPlaceholderConfigurer起的作用就是将占位符指向的数据库配置信息放在bean中定义的工具。