/**
* Retrieve the current target DataSource. Determines the
* {@link #determineCurrentLookupKey() current lookup key}, performs
* a lookup in the {@link #setTargetDataSources targetDataSources} map,
* falls back to the specified
* {@link #setDefaultTargetDataSource default target DataSource} if necessary.
* @see #determineCurrentLookupKey()
*/
protected DataSource determineTargetDataSource() {
Assert.notNull(this.resolvedDataSources, "DataSource router not initialized");
Object lookupKey = determineCurrentLookupKey();
DataSource dataSource = this.resolvedDataSources.get(lookupKey);
if (dataSource == null && (this.lenientFallback || lookupKey == null)) {
dataSource = this.resolvedDefaultDataSource;
}
if (dataSource == null) {
throw new IllegalStateException("Cannot determine target DataSource for lookup key [" + lookupKey + "]");
}
return dataSource;
}
/**
* Created by huangyangquan on 2016/11/30.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface DataSource {
DataSourceType value();
}
/**
* Created by huangyangquan on 2016/11/30.
*/
public enum DataSourceType {
MASTER,
SLAVE
}
/**
* Created by huangyangquan on 2016/11/30.
*/
public class DynamicDataSource extends AbstractRoutingDataSource {
@Override
protected Object determineCurrentLookupKey() {
return DynamicDataSourceHolder.getDataSourceType();
}
}
/**
* Created by huangyangquan on 2016/11/30.
*/
public class DynamicDataSourceHolder {
// 线程本地环境
private static final ThreadLocal<DataSourceType> contextHolder = new ThreadLocal<DataSourceType>();
// 设置数据源类型
public static void setDataSourceType(DataSourceType dataSourceType) {
Assert.notNull(dataSourceType, "DataSourceType cannot be null");
contextHolder.set(dataSourceType);
}
// 获取数据源类型
public static DataSourceType getDataSourceType() {
return (DataSourceType) contextHolder.get();
}
// 清除数据源类型
public static void clearDataSourceType() {
contextHolder.remove();
}
}
<bean id="spyGhotelDataSource" class="com.aheizi.config.DynamicDataSource">
<property name="targetDataSources">
<map key-type="java.lang.String">
<entry key="MASTER" value-ref="TEST-MASTER-DB"></entry>
<entry key="SLAVE" value-ref="TEST-SLAVE-DB"></entry>
</map>
</property>
<property name="defaultTargetDataSource" ref="TEST-MASTER-DB">
</property>
</bean>
<aop:aspectj-autoproxy proxy-target-class="false" />
<bean id="manyDataSourceAspect" class="com.aheizi.config.DataSourceAspect" />
<aop:config>
<aop:aspect id="dataSourceCut" ref="manyDataSourceAspect">
<aop:pointcut expression="execution(* com.aheizi.dao.*.*(..))"
id="dataSourceCutPoint" /><!-- 配置切点 -->
<aop:before pointcut-ref="dataSourceCutPoint" method="before" />
</aop:aspect>
</aop:config>
/**
* Created by huangyangquan on 2016/11/30.
*/
public class DataSourceAspect {
private static final Logger LOG = LoggerFactory.getLogger(DataSourceAspect.class);
public void before(JoinPoint point){
Object target = point.getTarget();
String method = point.getSignature().getName();
Class<?>[] classz = target.getClass().getInterfaces();
Class<?>[] parameterTypes = ((MethodSignature) point.getSignature()).getMethod().getParameterTypes();
try {
Method m = classz[0].getMethod(method, parameterTypes);
if (m != null && m.isAnnotationPresent(DataSource.class)) {
// 访问mapper中的注解
DataSource data = m.getAnnotation(DataSource.class);
switch (data.value()) {
case MASTER:
DynamicDataSourceHolder.setDataSourceType(DataSourceType.MASTER);
LOG.info("using dataSource:{}", DataSourceType.MASTER);
break;
case SLAVE:
DynamicDataSourceHolder.setDataSourceType(DataSourceType.SLAVE);
LOG.info("using dataSource:{}", DataSourceType.SLAVE);
break;
}
}
} catch (Exception e) {
LOG.error("dataSource annotation error:{}", e.getMessage());
// 若出现异常,手动设为主库
DynamicDataSourceHolder.setDataSourceType(DataSourceType.MASTER);
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有