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

本文介绍了如何在Spring环境中配置并读取jdbc.properties文件中的数据库连接信息,包括使用PropertyPlaceholderConfigurer进行属性占位符配置及c3p0数据源的具体设置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

时间: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的文章: 点击打开链接

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值