---
title: "PropertyPlaceholderConfigurer的用法："
author: "1enny"
date: "2015-03-13T16:43:00Z"
tags: []
slug: "propertyplaceholderconfigurer"
draft: false
lastmod: "2015-03-14T16:50:11Z"
id: 2192328
url: "https://1enny.iteye.com/blog/2192328"
---

# [PropertyPlaceholderConfigurer的用法：](http://www.cnblogs.com/wanggd/p/3172042.html)

用法1:

&lt;?xml version="1.0" encoding="UTF-8"?&gt;

&lt;beans

xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:p="http://www.springframework.org/schema/p"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"&gt;

&lt;!-- 使用spring提供的PropertyPlaceholderConfigurer读取数据库配置信息.properties--&gt;

&lt;bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt;

&lt;property name="locations"&gt;

&lt;list&gt;

&lt;!—

1. 这里的classpath可以认为是项目中的src-
2. 属性名是locations，使用子标签&lt;list&gt;&lt;/list&gt;可以指定多个数据库的配置文件，这里指定了一个

-&gt;

&lt;value&gt;classpath:resource/config/jdbc.properties&lt;/value&gt;

&lt;/list&gt;

&lt;/property&gt;

&lt;/bean&gt;

此时的数据库配置文件项目路径是这样的

![](http://images.cnitblog.com/blog/464665/201307/04174114-7e2a0807b00a4318b0237048b008ef11.jpg)

用法2:

读取数据库的配置文件还可以使用下面的方式

&lt;?xml version="1.0"encoding="UTF-8"?&gt;

&lt;beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"&gt;

&lt;bean id="propertyConfigurer"class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt;

&lt;property name="locations"&gt;

&lt;list&gt;

&lt;value&gt;/WEB-INF/config\_test/jdbc.properties&lt;/value&gt;

&lt;/list&gt;

&lt;/property&gt;

&lt;/bean&gt;

此时jdbc.properties文件的位置如下图所示

![](http://images.cnitblog.com/blog/464665/201307/04174220-4e80e8fbc7ed4888bfc5a811ee48f307.jpg)

.properties配置文件还可以有多个,这里在&lt;list&gt;&lt;/list&gt;标签中指定了2个数据的配置文件

&lt;?xml version="1.0"encoding="UTF-8"?&gt;

&lt;beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"&gt;

&lt;bean id="propertyConfigurer"class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt;

&lt;property name="locations"&gt;

&lt;list&gt;

&lt;value&gt;classpath:jdbc.properties&lt;/value&gt;

&lt;value&gt;/WEB-INF/config\_test/jdbc.properties&lt;/value&gt;

&lt;/list&gt;

&lt;/property&gt;

&lt;/bean&gt;

classpath:jdbc.properties对应的文件位置是：

![](http://images.cnitblog.com/blog/464665/201307/04174912-6857eff2008c4939b49071d7c14bf7f4.jpg)

文件内容是：配置的是sqlserver的连接信息

sqlserver.username=sa

sqlserver.password=sqlserver

sqlserver.url=jdbc\\:jtds\\:sqlserver\\://localhost\\:1433/J2EE

sqlserver.driver=net.sourceforge.jtds.jdbc.Driver

/WEB-INF/config\_test/jdbc.properties对应的文件位置是

![](http://images.cnitblog.com/blog/464665/201307/04175017-daffc4796cf54ac4b68a10252f543945.jpg)

文件内容是：配置的是oracle的连接信息

jdbc.driverClassName=oracle.jdbc.driver.OracleDriver

jdbc.url=jdbc:oracle:thin:@127.0.0.1:1521:orcl

jdbc.username=jxbms

jdbc.password=jxbms

这样数据库的配置信息被读取之后，在创建datasource的时候就可以使用了

下面连接oracle使用apache的dbcp数据源

&lt;bean id="dataSource"

class="org.apache.commons.dbcp.BasicDataSource"

destroy-method="close"&gt;

&lt;property name="driverClassName"&gt;

&lt;value&gt;${jdbc.driverClassName}&lt;/value&gt;

&lt;/property&gt;

&lt;property name="url"&gt;

&lt;value&gt;${jdbc.url}&lt;/value&gt;

&lt;/property&gt;

&lt;property name="username"&gt;

&lt;value&gt;${jdbc.username}&lt;/value&gt;

&lt;/property&gt;

&lt;property name="password"&gt;

&lt;value&gt;${jdbc.password}&lt;/value&gt;

&lt;/property&gt;

&lt;property name="maxActive"&gt;

&lt;value&gt;100&lt;/value&gt;

&lt;/property&gt;

&lt;property name="maxIdle"&gt;

&lt;value&gt;3&lt;/value&gt;

&lt;/property&gt;

&lt;property name="maxWait"&gt;

&lt;value&gt;-1&lt;/value&gt;

&lt;/property&gt;

&lt;propertyname="defaultAutoCommit"&gt;

&lt;value&gt;false&lt;/value&gt;

&lt;/property&gt;

&lt;/bean&gt;

下面连接sqlserver数据库使用的是c3p0数据源

&lt;bean id="dataSource\_oracle"class="com.mchange.v2.c3p0.ComboPooledDataSource"

destroy-method="close"&gt;

&lt;property name="driverClass"value="${jdbc.driverClassName}"/&gt;

&lt;property name="jdbcUrl"value="${jdbc.url}"/&gt;

&lt;property name="user"value="${jdbc.username}"/&gt;

&lt;property name="password" value="${jdbc.password}"/&gt;

&lt;/bean&gt;

使用dbcp数据源令人郁闷的事，使用dbcp和spring提供的JdbcTemplate操作数据库是查询是可以的

但是执行update、delete、insert into操作时，数据库中的数据没有变化

从网上查询了很多的资料，都无果。最后偶然看到网上有人说，dbcp数据源的事务不会自动提交，

当改成c3p0数据源后好了

随后认为这下终于可以松口气了，谁知道天不遂人愿。当更换一张表进行测试，数据库中的数据还是没有变化，难道c3p0数据源也不好使，

当再次经过代码的折磨之后，

最终发现改动测试java文件，不在一个项目中，把其他的项目关闭就好了

当文档写到这里时，突然发现oracle使用的dbcp数据源有这一项配置

&lt;propertyname="defaultAutoCommit"&gt;

&lt;value&gt;false&lt;/value&gt;

&lt;/property&gt;

原来dbcp数据源事务的自动提交功能被关闭了

马上把事务自动提交改成true进行测试，一切ok，(^ _ ^)