spring 配置多个数据源的文件

<?xml version="1.0" encoding="UTF-8"?>
<!-- Repository and Service layers -->
<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:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd">

    <!-- ========================= Repository RESOURCE DEFINITIONS ========================= -->
    <bean id="slave" class="org.apache.tomcat.jdbc.pool.DataSource">
        <property name="driverClassName" value="${jdbc.mysql.Driver}"/>
        <property name="url" value="${slave.connection}"/>
        <property name="username" value="${slave.username}"/>
        <property name="password" value="${slave.password}"/>
        <!--公共配置属性 -->
        <property name="maxActive" value="100"/>
        <property name="initialSize" value="10"/>
        <property name="minIdle" value="10"/>
        <property name="jdbcInterceptors" value="${tomcat.jdbc.pool.jdbcInterceptors}"/>
        <property name="testWhileIdle" value="true"/>
        <property name="testOnBorrow" value="true"/>
        <property name="validationQuery" value="select 1"/>
        <property name="testOnReturn" value="false"/>
        <property name="validationInterval" value="30000"/>
        <property name="timeBetweenEvictionRunsMillis" value="5000"/>
        <property name="maxWait" value="15000"/>
        <property name="removeAbandoned" value="true"/>
        <property name="removeAbandonedTimeout" value="60"/>
        <property name="logAbandoned" value="false"/>
        <property name="minEvictableIdleTimeMillis" value="30"/>
    </bean>

    <bean id="master" class="org.apache.tomcat.jdbc.pool.DataSource">
        <property name="driverClassName" value="${jdbc.mysql.Driver}"/>
        <property name="url" value="${master.connection}"/>
        <property name="username" value="${master.username}"/>
        <property name="password" value="${master.password}"/>
        <!--公共配置属性 -->
        <property name="maxActive" value="100"/>
        <property name="initialSize" value="10"/>
        <property name="minIdle" value="10"/>
        <property name="jdbcInterceptors" value="${tomcat.jdbc.pool.jdbcInterceptors}"/>
        <property name="testWhileIdle" value="true"/>
        <property name="testOnBorrow" value="true"/>
        <property name="validationQuery" value="select 1"/>
        <property name="testOnReturn" value="false"/>
        <property name="validationInterval" value="30000"/>
        <property name="timeBetweenEvictionRunsMillis" value="5000"/>
        <property name="maxWait" value="15000"/>
        <property name="removeAbandoned" value="true"/>
        <property name="removeAbandonedTimeout" value="60"/>
        <property name="logAbandoned" value="false"/>
        <property name="minEvictableIdleTimeMillis" value="30"/>
    </bean>

    <bean id="oldmemberdb" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="${oldmemberdb.connection}"/>
        <property name="username" value="${oldmemberdb.username}" />
        <property name="password" value="${oldmemberdb.password}" />
    </bean>


    <bean id="dataSource" class="db.DynamicDataSource">
        <property name="master" ref="master"/>
        <property name="slaves">
            <list>
                <ref bean="slave"/>
            </list>
        </property>
    </bean>

    <!-- ibatis3 工厂类 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="configLocation" value="classpath:sqlMapConfig.xml"/>
        <property name="mapperLocations"
                  value="classpath:/jdbc/uc/dao/*.xml"/>
        <property name="typeAliasesPackage" value="uc.jdbc.uc.po"/>
    </bean>

    <bean id="sqlSessionFactory_1" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="oldmemberdb"/>
        <property name="configLocation" value="classpath:sqlMapConfig.xml"/>
        <property name="mapperLocations"
                  value="classpath:/uc/jdbc/uc/dao/TestMapper.xml"/>
        <property name="typeAliasesPackage" value="uc.jdbc.uc.po"/>
    </bean>

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="uc.jdbc.uc.olddao"/>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory_1"></property>
    </bean>


    <bean class="MapperScannerConfigurer">
        <property name="basePackage" value="uc.jdbc.uc.dao"/>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
    </bean>

    <!-- 定义单个jdbc数据源的事务管理器 -->
    <bean id="transactionManager"
          class="DynamicDataSourceTransactionManager">
        <!-- class="org.springframework.jdbc.datasource.DataSourceTransactionManager">   -->
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <!-- 以 @Transactional 标注来定义事务 -->
    <tx:annotation-driven transaction-manager="trasactionManager"
                          proxy-target-class="true"/>

    <bean id="transactionTemplate"
          class="org.springframework.transaction.support.TransactionTemplate">
        <property name="transactionManager" ref="transactionManager"/>
        <!--ISOLATION_DEFAULT 表示由使用的数据库决定  -->
        <property name="isolationLevelName" value="ISOLATION_DEFAULT"/>
        <property name="propagationBehaviorName" value="PROPAGATION_REQUIRED"/>
        <property name="timeout" value="300"/>
    </bean>

    <bean id="transactionTemplateNew"
          class="org.springframework.transaction.support.TransactionTemplate">
        <property name="transactionManager" ref="transactionManager"/>
        <!--ISOLATION_DEFAULT 表示由使用的数据库决定  -->
        <property name="isolationLevelName" value="ISOLATION_DEFAULT"/>
        <property name="propagationBehaviorName" value="PROPAGATION_REQUIRES_NEW"/>
        <property name="timeout" value="300"/>
    </bean>

</beans>

posted @ 2016-11-08 15:15  (二少)在南极  阅读(1114)  评论(0编辑  收藏  举报