master-slave-db-selector
使用方法,配置模板
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<!-- 配置写数据源 -->
<bean id="masterDataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
<property name="driverClassName" value="${bridgeli.jdbc.driver}" />
<property name="url" value="${bridgeli.jdbc.url}" />
<property name="username" value="${bridgeli.jdbc.username}" />
<property name="password" value="${bridgeli.jdbc.password}" />
<property name="validationQuery" value="${bridgeli.jdbc.validationQuery}" />
<property name="initialSize" value="1" />
<property name="maxActive" value="20" />
<property name="minIdle" value="0" />
<property name="maxWait" value="60000" />
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" />
<property name="testWhileIdle" value="true" />
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<property name="minEvictableIdleTimeMillis" value="25200000" />
<property name="removeAbandoned" value="true" />
<property name="removeAbandonedTimeout" value="1800" />
<property name="logAbandoned" value="true" />
<property name="filters" value="stat" />
</bean>
<!-- 配置读数据源 -->
<bean id="parentSlaveDataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
<property name="driverClassName" value="${bridgeli.jdbc.driver}" />
<property name="validationQuery" value="${bridgeli.jdbc.validationQuery}" />
<property name="initialSize" value="1" />
<property name="maxActive" value="20" />
<property name="minIdle" value="0" />
<property name="maxWait" value="60000" />
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" />
<property name="testWhileIdle" value="true" />
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<property name="minEvictableIdleTimeMillis" value="25200000" />
<property name="removeAbandoned" value="true" />
<property name="removeAbandonedTimeout" value="1800" />
<property name="logAbandoned" value="true" />
<property name="filters" value="stat" />
</bean>
<bean id="slaveDataSource1" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close" parent="parentSlaveDataSource">
<property name="url" value="${bridgeli_slave1.jdbc.url}" />
<property name="username" value="${bridgeli_slave1.jdbc.username}" />
<property name="password" value="${bridgeli_slave1.jdbc.password}" />
</bean>
<bean id="dataSource" class="cn.bridgeli.masterslavedbselector.MasterSlaveDataSource">
<property name="targetDataSources">
<map>
<entry key-ref="masterDataSource" value-ref="masterDataSource"/>
<entry key-ref="slaveDataSource1" value-ref="slaveDataSource1"/>
</map>
</property>
<property name="defaultTargetDataSource" ref="masterDataSource"/>
<property name="masterSlaveSelector" ref="dataSelector"/>
</bean>
<bean id="dataSelector" class="cn.bridgeli.masterslavedbselector.MasterSlaveSelectorByPoll">
<property name="masters">
<list>
<ref bean="masterDataSource"/>
</list>
</property>
<property name="slaves">
<list>
<ref bean="slaveDataSource1"/>
</list>
</property>
<property name="defaultDataSource" ref="masterDataSource"/>
</bean>
<aop:aspectj-autoproxy/>
<!-- mybaits 数据工厂 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 自动扫描所有注解的路径 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.bridgeli.mapper" />
<!-- <property name="sqlSessionFactory" ref="sqlSessionFactory" /> -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
<!-- 数据库切面 -->
<bean id="masterSlaveAspect" class="cn.bridgeli.masterslavedbselector.MasterSlaveAspect">
<property name="prefixMasters">
<list>
<value>save</value>
<value>update</value>
<value>delete</value>
</list>
</property>
</bean>
<aop:config>
<aop:aspect id="c" ref="masterSlaveAspect">
<aop:pointcut id="tx" expression="execution(* cn.bridgeli.service..*.*(..))"/>
<aop:before pointcut-ref="tx" method="before"/>
</aop:aspect>
</aop:config>
<context:annotation-config />
<context:component-scan base-package="cn.bridgeli" />
</beans>