【笔记】Spring4框架系列 [ 10 ] 之 Spring配置文件中注册三种数据源

Spring配置文件中注册三种数据源及从属性文件中读取DB连接四要素

【applicationContext.xml】


<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">


    
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <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>

    
    <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>

    
    <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>
    
    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="classpath:jdbc.properties" />
    bean>

    
    <context:property-placeholder location="classpath:jdbc.properties" />


    <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" />  
       
        <property name="maxIdleTime" value="30"/>  
        
        <property name="maxPoolSize" value="20"/>  
        
        <property name="minPoolSize" value="3"/>  
        
        <property name="maxIdleTimeExcessConnections" value="15"/>
    bean>


    <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" />
        
        
    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

源码下载:http://download.csdn.net/detail/jul_11th/9754505

谢谢支持!

你可能感兴趣的:(Spring,Spring,4,框架系列)