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

spring配置文件读取jdbc.properties的配置信息

2016-12-03 14:33 846 查看
时间:2016-12-3


内容:spring读取jdbc.properties的配置信息;

一.jdbc.properties的配置信息

jdbc.properties的配置信息主要包括用户名、密码以及连接池的一些配置,代码如下:

jdbc.driverClassName=com.mysql.jdbc.Driver
#jdbc.url=jdbc\:mysql\://172.18.73.253\:3307/network
jdbc.url=jdbc\:mysql\://127.0.0.1\:3306/webwork
#jdbc.url=jdbc\:mysql\://172.18.73.5\:3306/webwork
jdbc.username=root
jdbc.password=root
jdbc.initialPoolSize=20
jdbc.maxPoolSize=100
jdbc.minPoolSize=10
jdbc.maxIdleTime=600
jdbc.acquireIncrement=5
jdbc.maxStatements=50
jdbc.idleConnectionTestPeriod=60

二、 在applicationContext.xml中怎么读取jdbc.properties的配置信息;

废话少说,直接上代码,代码如下:

<?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"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd"> <!-- 加载jdbc.properties配置文件 -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:jdbc.properties</value>
</list>
</property>
</bean>

<!-- c3p0数据源配置方式-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close"
p:driverClass="${jdbc.driverClassName}"
p:jdbcUrl="${jdbc.url}"
p:user="${jdbc.username}"
p:password="${jdbc.password}"
p:maxPoolSize="${jdbc.maxPoolSize}"
p:minPoolSize="${jdbc.minPoolSize}"
p:initialPoolSize="${jdbc.initialPoolSize}"
p:maxIdleTime="${jdbc.maxIdleTime}"
p:acquireIncrement="${jdbc.acquireIncrement}"
p:maxStatements="${jdbc.maxStatements}"
p:idleConnectionTestPeriod="${jdbc.idleConnectionTestPeriod}"
/>

在此推荐一篇spring读取jdbc.properties的文章: 点击打开链接
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: