Spring配置文件中注册三种数据源及从属性文件中读取DB连接四要素
【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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 注册数据源一:Spring内置数据源 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<!-- <property name="url" value="jdbc:mysql:///athl_ajax" /> -->
<property name="url" value="jdbc:mysql://localhost:3306/athl_ajax" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
<!-- 注册数据源二:DBCP数据源
导入org.apache.commons.pool.jar和org.apache.commons.dbcp.jar下方下载链接中有!
-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/athl_ajax" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
<!-- 注册数据源三:C3P0数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/athl_ajax"/>
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="user" value="root"/>
<property name="password" value="root"/>
</bean>
<bean id="properties_dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- 注册jdbc属性文件(1) -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties" />
</bean>
<!-- 注册jdbc属性文件(2)需要添加context约束
spring-framework-4.2.1.RELEASE\docs\spring-framework-reference\html\xsd-configuration.html
40.2.8
-->
<context:property-placeholder location="classpath:jdbc.properties" />
<!-- mysql -->
<bean id="mysql_dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/athl_ajax"/>
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="user" value="root"/>
<property name="password" value="root"/>
<property name="initialPoolSize" value="3" />
<!-- 连接的最大空闲时间 单位秒 默认是0-代表永远不会断开连接 超过设定时间的空闲连接将会断开 -->
<property name="maxIdleTime" value="30"/>
<!-- 连接池中拥有的最大连接数 默认值为15个 -->
<property name="maxPoolSize" value="20"/>
<!-- 连接池中保持的最小连接数 默认值为3个 -->
<property name="minPoolSize" value="3"/>
<!-- 将连接池的连接数保持在minpoolsize 必须小于maxIdleTime设置 默认值为0代表不处理 单位秒 -->
<property name="maxIdleTimeExcessConnections" value="15"/>
</bean>
<!-- oracle -->
<bean id="oracle_dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="oracle.jdbc.driver.OracleDriver"></property>
<property name="jdbcUrl" value="jdbc:oracle:thin:@localhost:1521:orcl"></property>
<property name="user" value="scott"></property>
<property name="password" value="scott"></property>
<property name="initialPoolSize" value="3"></property>
<property name="maxPoolSize" value="10"></property>
<property name="maxStatements" value="100"></property>
<property name="acquireIncrement" value="2"></property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="properties_dataSource" />
<!-- <property name="dataSource" ref="mysql_dataSource" /> -->
<!-- <constructor-arg name="dataSource" ref="oracle_dataSource" /> -->
</bean>
<bean id="dao" class="com.athl.dao.PersonDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate"/>
</bean>
<bean id="service" class="com.athl.service.PersonServiceImpl">
<property name="dao" ref="dao"/>
</bean>
<bean id="action" class="com.athl.action.PersonAction" >
<property name="service" ref="service" />
</bean>
</beans>
【jdbc.properties】
放在src下
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/athl_ajax
jdbc.user=root
jdbc.password=root