import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
public class DataSources extends AbstractRoutingDataSource{
@Override
protected Object determineCurrentLookupKey() {
return DataSourceSwitch.getDataSourceType();
}
}
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import java.lang.annotation.ElementType;
import java.lang.annotation.RetentionPolicy;
/**
*@Title:
*@Description: 切换数据源注解
*@Author:lijianji
*@Since:2016年4月25日 上午11:06:02
*@Version:1.0
*/
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface DataSourceSwitchAnnotation {
}
import java.lang.reflect.Method;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*@Title:
*@Description:织入切入点
*@Author:lijianji@jd.com
*@Since:2016年4月25日 上午11:06:29
*@Version:1.0
*/
@Aspect
public class DataSourceSwitchAspect {
private Logger logger = LoggerFactory.getLogger(DataSourceSwitchAspect.class);
@Pointcut("@annotation(com.xxx.DataSourceSwitchAnnotation)")
public void proxyAspect() {
}
@Around("proxyAspect()")
public Object aroundExec(ProceedingJoinPoint joinPoint) {
MethodSignature ms=(MethodSignature) joinPoint.getSignature();
Method method=ms.getMethod();
String methodName=method.getName();
Object result=null;
try {
if(true/*什么条件*/){
DataSourceSwitch.setDataSourceType(DataSourceSwitch.CDS);
logger.debug("DataSourceSwitch.CDS:{}",methodName);
}
result = joinPoint.proceed();
} catch (Throwable e) {
logger.info("errorMsg",e);
}
// finally{
// DataSourceSwitch.setDataSourceType(DataSourceSwitch.MYSQL);
// logger.info("DataSourceSwitch.MYSQL:{}",methodName);
// }
return result;
}
}
public class DataSourceSwitch{
public static final String MYSQL="MYSQL";
public static final String CDS="CDS";
private static final ThreadLocal contextHolder=new ThreadLocal();
public static void setDataSourceType(String dataSourceType){
contextHolder.set(dataSourceType);
}
public static String getDataSourceType(){
return (String) contextHolder.get();
}
public static void clearDataSourceType(){
contextHolder.remove();
}
}
public class Demo {
public Demo() {
}
/**
* @param args
* @Description:
*/
public static void main(String[] args) {
DataSourceSwitch.setDataSourceType(DataSourceSwitch.MYSQL);
// Dao.insert();
}
@DataSourceSwitchAnnotation
public void service(){
// Dao.insert();
}
}
spring-datasource.xml
<?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:context="http://www.springframework.org/schema/context"
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/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"
default-autowire="byName">
<aop:aspectj-autoproxy />
<context:annotation-config/>
<context:component-scan base-package="com.xxx"/>
<bean class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator" />
<bean id="dataSourceSwitchAspect" class="com.jd.insurance.pay.common.multiDataSource.DataSourceSwitchAspect" />
<bean id="mysqlDataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="" />
<property name="url" value="" />
<property name="username" value="" />
<property name="password" value="" />
<property name="maxActive" value="20" />
<property name="maxIdle" value="10" />
<property name="minIdle" value="5" />
<property name="initialSize" value="3" />
<property name="maxWait" value="1500" />
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<property name="minEvictableIdleTimeMillis" value="180000" />
</bean>
<bean id="cdsDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="" />
<property name="url" value="${cds.url}" />
<property name="username" value="ins_business" />
<property name="password" value="ins_business" />
</bean>
<bean id="commonDataSource" class="com.xxx.DataSources"> <!-- DataSources 的路径 -->
<property name="targetDataSources">
<map key-type="java.lang.String">
<entry value-ref="mysqlDataSource" key="MYSQL"></entry>
<entry value-ref="cdsDataSource" key="CDS"></entry>
</map>
</property>
<property name="defaultTargetDataSource" ref="mysqlDataSource"></property>
</bean>
<!-- mybatis setting-->
<bean id="commonSqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="commonDataSource"/>
<property name="typeAliasesPackage" value="com.xxx.po"/>
</bean>
<bean id="commonMapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="commonSqlSessionFactory"/>
<property name="basePackage" value="com.xxx.dao"/>
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="commonDataSource"></property>
</bean>
</beans>