1 比如我们在sc目录下新建一个db.properties文件内容如下
DriverClass=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test
username = root
password =root
2把配置文件交给容器管理在applicationContext.xml加上这一句
<context:property-placeholder location="classpath:db.properties" file-encoding="utf-8"/>
3然后配置数据源的时候可以直接引用
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${DriverClass}"></property>
<property name="url" value="${url}"></property>
<property name="username" value="${username}"></property>
<property name="password" value="${password}"></property>
</bean>
第二种方式
根据bean注入
<bean id="propetiesConfig" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
<property name="location" value="classpath:db/dbccc.properties"></property>
<property name="locations">
<list>
<value>classpath:db.properties</value>
</list>
</property>
<property name="fileEncoding" value="UTF-8"></property>
<property name="ignoreResourceNotFound" value="true"></property>
</bean>