spring中的PropertyPlaceholderConfigurer访问properties文件配置

来源:互联网 发布:js 表单如何拼接字符串 编辑:程序博客网 时间:2024/06/04 21:06
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.Driverurl=jdbc\:mysql\://localhost\:3306/testusername=rootpassword=root

0 0
原创粉丝点击