---
title: "Spring框架上资源属性的配置器类PropertyPlaceholderConfigurer的用法--动态链接数据库"
author: "xiaoyixiao"
date: "2015-04-03T11:14:35Z"
tags: ["Bean", "Spring", "框架"]
categories: ["编程语言"]
slug: "spring-propertyplaceholderconfigurer"
draft: false
lastmod: "2015-04-03T11:14:35Z"
id: 2198627
url: "https://xiaoyixiao.iteye.com/blog/2198627"
---

applicationContext.xml配置

&lt;!-- 定义数据源Bean，使用DBCP数据源实现 --&gt;

 

&lt;bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"&gt;

 

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

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

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

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

    &lt;property name="maxActive" value="30"/&gt;  

    &lt;property name="maxIdle" value="${dbcp.maxIdle}" /&gt;   

    &lt;property name="maxWait" value="${dbcp.maxWait}" /&gt;   

    &lt;property name="defaultAutoCommit" value="${dbcp.defaultAutoCommit}"/&gt;   

    &lt;property name="removeAbandoned" value="${dbcp.removeAbandoned}"/&gt;   

    &lt;property name="removeAbandonedTimeout" value="${dbcp.removeAbandonedTimeout}"/&gt;   

    &lt;property name="logAbandoned" value="${dbcp.logAbandoned}"/&gt; 

 

&lt;/bean&gt;以${}方式读取properties

 

 

来看一个Bean定义档的实际例子： 

beans-config.xml 

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

&lt;!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN" "http://www.springframework.org/dtd/spring-beans.dtd"&gt; 

 

&lt;beans&gt;  

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

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

                &lt;list&gt;

                    &lt;value&gt;jdbc/${param}/hello.properties&lt;/value&gt; 

                &lt;/list&gt;

            &lt;/property&gt;

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

             ......

            &lt;/property&gt;

        &lt;/bean&gt; 

&lt;/beans&gt;

另外可以通过propertiesPersister对数据库密码加解密

实际上，PropertyPlaceholderConfigurer起的作用就是将占位符指向的数据库配置信息放在bean中定义的工具。