1. 程式人生 > >spring配置檔案讀取jdbc.properties的配置資訊

spring配置檔案讀取jdbc.properties的配置資訊

時間: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的文章: 點選開啟連結