data-source.xml 6.71 KB
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns="http://www.springframework.org/schema/beans"
       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.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

    <!-- DBCP连接池 -->
    <!--<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
        <property name="driverClassName" value="${jdbc.driver}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
        <property name="defaultAutoCommit" value="false" />
        <property name="filters" value="config" />
        <property name="connectionProperties" value="config.decrypt=true" />
        <property name="maxActive" value="200" />
        &lt;!&ndash;<property name="connectionInitSqls" value="set names utf8mb4;"/>&ndash;&gt;
    </bean>-->
    <!-- ======================================================================== -->
    <!-- MySQL 主库连接配置 -->
    <!-- ======================================================================== -->
    <bean id="masterDataSource" name="masterDataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
        <property name="driverClassName" value="${jdbc.mysql.master.driver}" />
        <property name="url" value="${jdbc.mysql.master.url}" />
        <property name="username" value="${jdbc.mysql.master.username}" />
        <property name="password" value="${jdbc.mysql.master.password}" />
        <property name="defaultAutoCommit" value="false" />
        <property name="filters" value="config" />
        <property name="maxActive" value="400" />
        <property name="connectionProperties" value="config.decrypt=true" />
    </bean>

    <!-- ======================================================================== -->
    <!-- MySQL 从库连接配置 -->
    <!-- ======================================================================== -->
    <bean id="slaveDataSource" name="slaveDataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
        <property name="driverClassName" value="${jdbc.mysql.slave.driver}" />
        <property name="url" value="${jdbc.mysql.slave.url}" />
        <property name="username" value="${jdbc.mysql.slave.username}" />
        <property name="password" value="${jdbc.mysql.slave.password}" />
        <property name="defaultAutoCommit" value="false" />
        <property name="filters" value="config" />
        <property name="maxActive" value="400" />
        <property name="connectionProperties" value="config.decrypt=true" />
    </bean>
    <!-- ======================================================================== -->
    <!-- 连接配置 -->
    <!-- ======================================================================== -->
    <bean id="dataSource" class="com.xiniunet.framework.data.MultipleDataSource">
        <property name="defaultTargetDataSource" ref="masterDataSource"/>
        <property name="targetDataSources">
            <map>
                <entry key="masterDataSource" value-ref="masterDataSource"/>
                <entry key="slaveDataSource" value-ref="slaveDataSource"/>
            </map>
        </property>
    </bean>
    <!--  TransactionManager定义 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <!-- 使用annotation定义事务 -->
    <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />

    <!-- MyBatis配置 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <!-- 自动扫描模型对象目录, 省掉Configuration.xml里的手工配置, 改为明确的控制
        <property name="typeAliasesPackage" value="com.xniu.xxx.contract" /> -->
        <!-- 指定Mapper文件位置 -->
        <property name="mapperLocations" value="classpath:/mapper/*/*.xml"/>
        <property name="configLocation"  value="classpath:/mybatis_setting.xml" />
        <property name="configurationProperties">
            <props>
                <prop key="mapUnderscoreToCamelCase">true</prop>
                <prop key="aggressiveLazyLoading">false</prop>
                <prop key="cacheEnabled">true</prop>
                <prop key="useColumnLabel">true</prop>
                <!--<prop key="useGeneratedKeys">true</prop>-->
            </props>
        </property>
    </bean>

    <!-- 扫描basePackage下所有以@MyBatisRepository标识的 接口-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.xiniunet.service.*.dal" />
        <property name="annotationClass" value="com.xiniunet.framework.annotation.MyBatisRepository"/>
    </bean>

    <bean id="dbPropertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="order" value="1" />
        <property name="ignoreUnresolvablePlaceholders" value="true" />
        <property name="locations">
            <list>
                <value>classpath*:/jdbc.properties</value>
                <value>classpath*:/setting.properties</value>
            </list>
        </property>
    </bean>

    <bean id="cacheManager" class="com.xiniunet.framework.cache.CacheManagerImpl">
        <constructor-arg value="${aliyun.ocs.host}"/>
        <constructor-arg value="${aliyun.ocs.port}"/>
        <constructor-arg value="${deploy.mode}"/>
        <constructor-arg value="${aliyun.ocs.username}"/>
        <constructor-arg value="${aliyun.ocs.password}"/>
    </bean>

    <bean id="cacheAop" class="com.xiniunet.framework.cache.CacheAop">
    </bean>

    <!-- 服务方法切面配置 -->
    <bean id="serviceMethodAspect" class="com.xiniunet.framework.aop.ServiceMethodAspect"/>
    <aop:config>
        <aop:pointcut id="pointcut" expression="execution(* com.xiniunet.service.railway.svc.*.*(..))"/>
        <aop:aspect id="dataSourceAspect" ref="serviceMethodAspect">
            <aop:before method="doBefore" pointcut-ref="pointcut"/>
            <aop:around method="doAround" pointcut-ref="pointcut"/>
            <aop:after method="doAfter" pointcut-ref="pointcut"/>
        </aop:aspect>
    </aop:config>
</beans>