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
来看一个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
相关文章推荐
- Spring整合HDFS
- 使用IntelliJ IDEA 14和Maven创建java web项目
- Shiro 整合 SpringMVC 配置文件详解篇
- blocker critical Java (转)
- Java Web学习(23): JSTL学习(一)
- Spring4笔记----使用外部属性文件配置 bean
- Java技巧--修改Eclipse默认Workspace路径
- JAVA二进制.位运算.移位运算
- java中判断字符串是否为数字的方法的几种方法
- Spring4笔记----bean的作用范围
- java线程安全总结
- JavaEE程序猿的免费午餐③
- Spring4 Junit异常浅析
- Shiro+Spring MVC整合
- JDK配置顺序
- Myeclipse使用DB Browser连接数据库错误:OPTION SQL_SELECT_LIMIT=DEFAULT
- Spring IOC注入源码分析
- Java中Cookie的使用方法
- maven+eclipse项目问题报错原因(Cannot change version of project facet Dynamic web module to 2.5)
- java事件处理机制(自定义事件)