<?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: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.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd ">
<!-- 配置数据源 -->
<bean name="datasource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<property name="url" value="${jdbc.urlmaster}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!-- 配置数据源1 -->
<bean name="datasource1" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<property name="url" value="${jdbc.urlslave}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<bean id="multipleDataSource" class="pers.aidenj.ostrich.common.DataSource.DynamicDataSource">
<property name="targetDataSources">
<map>
<entry key="datasource" value-ref="datasource"/>
<entry key="datasource1" value-ref="datasource1"/>
</map>
</property>
<property name="defaultTargetDataSource" ref="datasource"/> <!-- 默认数据源 -->
</bean>
<!-- 配置sqlSessionFactory 并读取mybatis的一些配置 -->
<bean name="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="multipleDataSource"></property>
<property name="mapperLocations" value="classpath:pers/aidenj/ostrich/mapping/*.xml" />
</bean>
<!-- 自动扫描 将Mapper接口生成代理注入到Spring -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="pers.aidenj.ostrich.mapper" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>
<!-- 配置事物 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="multipleDataSource"></property>
</bean>
<tx:annotation-driven transaction-manager = "transactionManager"/>
<!-- 事物的具体内容 -->
<tx:advice id="transactionAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="append*" propagation="REQUIRED" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="modify*" propagation="REQUIRED" />
<tx:method name="edit*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="remove*" propagation="REQUIRED" />
<tx:method name="repair" propagation="REQUIRED" />
<tx:method name="delAndRepair" propagation="REQUIRED" />
<tx:method name="get*" propagation="SUPPORTS" />
<tx:method name="find*" propagation="SUPPORTS" />
<tx:method name="load*" propagation="SUPPORTS" />
<tx:method name="search*" propagation="SUPPORTS" />
<tx:method name="datagrid*" propagation="SUPPORTS" />
<tx:method name="*" propagation="SUPPORTS" />
</tx:attributes>
</tx:advice>
<!-- 为业务逻辑层的方法解析@DataSource注解 为当前线程的routeholder注入数据源key -->
<bean id="dataSourceAspect" class="pers.aidenj.ostrich.common.aop.DataSourceAspect" />
<aop:config proxy-target-class="true">
<!-- 定义一个切面 -->
<aop:aspect id="dataSourceAspect" ref="dataSourceAspect" order="1">
<!-- 定义一个切点 -->
<aop:pointcut id="dataSourcePointcut" expression="execution(* pers.aidenj.ostrich.service.*.*(..)) "/>
<!-- 前通知 -->
<!-- <aop:before pointcut-ref="dataSourcePointcut" method="before" /> -->
<aop:before pointcut-ref="dataSourcePointcut" method="intercept" />
</aop:aspect>
</aop:config>
</beans>
package pers.aidenj.ostrich.common.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface DataSource {
String value();
}
package pers.aidenj.ostrich.common.aop;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.reflect.MethodSignature;
import java.lang.reflect.Method;
import pers.aidenj.ostrich.common.DataSource.DynamicDataSourceHolder;
import pers.aidenj.ostrich.common.annotation.DataSource;
public class DataSourceAspect {
/**
* 拦截目标方法,获取由@DataSource指定的数据源标识,设置到线程存储中以便切换数据源
*
* @param point
* @throws Exception
*/
public void intercept(JoinPoint point) throws Exception {
Class<?> target = point.getTarget().getClass();
MethodSignature signature = (MethodSignature) point.getSignature();
// 默认使用目标类型的注解,如果没有则使用其实现接口的注解
signature.getMethod().getParameterTypes();
for (Class<?> clazz : target.getInterfaces()) {
resolveDataSource(clazz, signature.getMethod());
}
resolveDataSource(target, signature.getMethod());
}
/**
* 提取目标对象方法注解和类型注解中的数据源标识
*
* @param clazz
* @param method
*/
private void resolveDataSource(Class<?> clazz, Method method) {
try {
Class<?>[] types = method.getParameterTypes();
// 默认使用类型注解
if (clazz.isAnnotationPresent(DataSource.class)) {
DataSource source = clazz.getAnnotation(DataSource.class);
DynamicDataSourceHolder.setDataSource(source.value());
}
// 方法注解可以覆盖类型注解
Method m = clazz.getMethod(method.getName(), types);
if (m != null && m.isAnnotationPresent(DataSource.class)) {
DataSource source = m.getAnnotation(DataSource.class);
DynamicDataSourceHolder.setDataSource(source.value());
}
} catch (Exception e) {
System.out.println(clazz + ":" + e.getMessage());
}
}
}
package pers.aidenj.ostrich.serviceImpl;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import pers.aidenj.ostrich.common.DataSource.DynamicDataSourceHolder;
import pers.aidenj.ostrich.common.annotation.DataSource;
import pers.aidenj.ostrich.mapper.DictionaryMapper;
import pers.aidenj.ostrich.model.Dictionary;
import pers.aidenj.ostrich.service.DictionaryService;
@Service("dictionaryService")
public class DictionaryServiceImpl implements DictionaryService {
@Resource
DictionaryMapper dictionaryMapper;
@Override
public boolean getOneData() {
// TODO Auto-generated method stub
return true;
}
//没有指定库,使用默认
@Override
public List<Dictionary> getAllDictionary() {
// TODO Auto-generated method stub
return dictionaryMapper.getAllDictionary();
}
//指定master
@Override
@DataSource("datasource")
public List<Dictionary> getAllDictionaryMasterDataSource() {
// TODO Auto-generated method stub
return dictionaryMapper.getAllDictionary();
}
//指定master
@Override
public List<Dictionary> getAllDictionaryMaster() {
// TODO Auto-generated method stub
DynamicDataSourceHolder.setDataSource("datasource");
return dictionaryMapper.getAllDictionary();
}
//指定slave
@Override
@DataSource("datasource1")
public List<Dictionary> getAllDictionarySlaveDataSource() {
// TODO Auto-generated method stub
return dictionaryMapper.getAllDictionary();
}
//指定slave
@Override
public List<Dictionary> getAllDictionarySlave() {
// TODO Auto-generated method stub
DynamicDataSourceHolder.setDataSource("datasource1");
return dictionaryMapper.getAllDictionary();
}
}