<?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:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans.xsd
                        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">



    <bean id="dataSourceAdplatform" class="com.alibaba.druid.pool.DruidDataSource"
        init-method="init" destroy-method="close">

        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="${spring.adplatform.jdbcUrl}" />
        <property name="username" value="wx2016_longvideo" />
        <property name="password" value="wx2016_longvideoP@assword1234" />
        <property name="maxActive" value="20" />
        <property name="initialSize" value="1" />
        <property name="maxWait" value="60000" />
        <property name="minIdle" value="3" />
        <property name="connectionProperties" value="config.decrypt=true;clientEncoding=UTF-8" />
        <property name="connectionInitSqls" value="set names utf8mb4;" />
        <property name="filters" value="stat" />
        <property name="timeBetweenEvictionRunsMillis" value="600000" />
        <property name="minEvictableIdleTimeMillis" value="300000" />
        <property name="validationQuery" value="select 1" />
        <property name="testWhileIdle" value="true" />
        <property name="testOnBorrow" value="false" />
        <property name="testOnReturn" value="false" />
        <property name="maxOpenPreparedStatements" value="20" />
        <property name="removeAbandoned" value="true" />
        <property name="removeAbandonedTimeout" value="1800" />
        <property name="logAbandoned" value="true" />
    </bean>

    <bean id="dataSourceOriginal" class="com.alibaba.druid.pool.DruidDataSource"
          init-method="init" destroy-method="close">

        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="${spring.datasource.jdbcUrl}" />
        <property name="username" value="${spring.datasource.username}" />
        <property name="password" value="${spring.datasource.password}" />
        <property name="maxActive" value="20" />
        <property name="initialSize" value="1" />
        <property name="maxWait" value="60000" />
        <property name="minIdle" value="3" />
        <property name="connectionProperties" value="config.decrypt=true;clientEncoding=UTF-8" />
        <property name="connectionInitSqls" value="set names utf8mb4;" />
        <property name="filters" value="stat" />
        <property name="timeBetweenEvictionRunsMillis" value="600000" />
        <property name="minEvictableIdleTimeMillis" value="300000" />
        <property name="validationQuery" value="select 1" />
        <property name="testWhileIdle" value="true" />
        <property name="testOnBorrow" value="false" />
        <property name="testOnReturn" value="false" />
        <property name="maxOpenPreparedStatements" value="20" />
        <property name="removeAbandoned" value="true" />
        <property name="removeAbandonedTimeout" value="1800" />
        <property name="logAbandoned" value="true" />
    </bean>

    <!--动态数据源的配置-->
    <bean id="dynamicDataSource" class=" com.tzld.piaoquan.ad.datasource.DynamicDataSource">
        <property name="targetDataSources">
            <map key-type="java.lang.String">
                <entry key="dataSourceAdplatform"  value-ref="dataSourceAdplatform" />
                <entry key="dataSourceOriginal"  value-ref="dataSourceOriginal" />
            </map>
        </property>
    </bean>
    
    <bean id="dataSourceChangeAspect" class="com.tzld.piaoquan.ad.datasource.DataSourceAspect">
        <property name="defaultDataSource" value="dataSourceOriginal"></property>
        <property name="targetDataSources">
            <map key-type="java.lang.String">
                <entry key="dataSourceAdplatform" value="dataSourceAdplatform"/>
                <entry key="dataSourceOriginal" value="dataSourceOriginal"/>
            </map>
        </property>
    </bean>

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dynamicDataSource" />
        <property name="configLocation" value="classpath:mybatis-config.xml" />
        <property name="mapperLocations" value="classpath:mapper/**/*.xml" />
    </bean>

    <!-- 指定Mapper接口所在的包 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
        <property name="basePackage" value="com.tzld.piaoquan.ad.dao.mapper" />
    </bean>


    <!-- 指定事务-->
    <bean id="transactionManager"  class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dynamicDataSource" />
    </bean>

    <!-- 一定要在事务之前切换数据源，不然则会失效-->
    <aop:config>
        <aop:pointcut id="serviceAop"  expression="execution(* com.tzld.piaoquan.ad.service..*.*(..))" />
        <aop:aspect ref="dataSourceChangeAspect" order="0">
            <aop:before method="doBefore" pointcut-ref="serviceAop"/>
            <aop:after-returning method="doAfterReturning" pointcut-ref="serviceAop"/>
        </aop:aspect>
    </aop:config>

    <!--配置事务模板对象     编程式事务 模板-->
    <bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
        <property name="transactionManager" ref="transactionManager"/>
    </bean>

</beans>
