您的位置:首页 > 编程语言 > Java开发

spring中的PropertyPlaceholderConfigurer访问properties文件配置

2016-07-28 10:02 295 查看
Spring的框架中提供了一个类: org.springframework.beans.factory.config.PropertyPlaceholderConfigurer。这个类,可以将一些经常需要改动的配置如用户名密码等,移至.properties文件中,而.properties文件可以作为客户根据需求,自定义一些相关的参数。

来看一个spring配置C3P0连接池的例子:

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

<bean id="config" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<!--单个properties文件采用以下写法-->
<property name="location">
<value>classpath:jdbc.properties</value>
</property>
<!--多个properties文件采用以下写法-->
<property name="locations">
<list>
<value>classpath:jdbc.properties</value>
<value>classpath:xxx.properties</value>
<value>classpath:xxx.properties</value>
</list>
</property>
</bean>
<!-- 配置c3p0连接池 -->
<bean id="comboPooledDataSourceID" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${driverClassName}"/>
<property name="jdbcUrl" value="${url}"/>
<property name="user" value="${username}"/>
<property name="password" value="${password}"/>
</bean>

</beans>
jdbc.properties

driverClassName=com.mysql.jdbc.Driver
url=jdbc\:mysql\://localhost\:3306/test
username=root
password=root
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: