多数据源,切面处理类
@Pointcut("@annotation(cn.ibona.datasources.annotation.MoreDataSource)")
public void dataSourcePointCut() {
}
@Around("dataSourcePointCut()")
public Object around(ProceedingJoinPoint point) throws Throwable {
MethodSignature signature = (MethodSignature) point.getSignature();
Method method = signature.getMethod();
MoreDataSource ds = method.getAnnotation(MoreDataSource.class);
if(ds == null){
DynamicDataSource.setDataSource(DataSourceNames.FIRST);
} else if (ds.value().equals(DataSourceNames.FIRST)){
DynamicDataSource.setDataSource(DataSourceNames.FIRST);
}else {
DynamicDataSource.setDataSource(ds.value());
}
try {
return point.proceed();
} finally {
DynamicDataSource.clearDataSource();
}
}
}
设置多数据源时,需要更改最初数据源的类型,否则会导致数据源寻找错误
设置数据源时谨慎手动配置默认数据源(血的教训。。。)
DynamicDataSource.setDataSource(“first”);
可能会导致查错数据库