|
@@ -12,6 +12,7 @@ import org.springframework.stereotype.Component;
|
|
import com.ruoyi.common.annotation.DataSource;
|
|
import com.ruoyi.common.annotation.DataSource;
|
|
import com.ruoyi.common.utils.StringUtils;
|
|
import com.ruoyi.common.utils.StringUtils;
|
|
import com.ruoyi.framework.datasource.DynamicDataSourceContextHolder;
|
|
import com.ruoyi.framework.datasource.DynamicDataSourceContextHolder;
|
|
|
|
+import org.aspectj.lang.JoinPoint;
|
|
|
|
|
|
/**
|
|
/**
|
|
* 多数据源处理
|
|
* 多数据源处理
|
|
@@ -32,13 +33,45 @@ public class DataSourceAspect
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
- @Before(value="dsPointCut() && @within(dataSource))", argNames="dataSource")
|
|
|
|
- public void before(DataSource dataSource){
|
|
|
|
- DynamicDataSourceContextHolder.setDataSourceType(dataSource.value().name());
|
|
|
|
|
|
+ @Before("dsPointCut()")
|
|
|
|
+ public void before(JoinPoint point)
|
|
|
|
+ {
|
|
|
|
+ DataSource dataSource = getDataSource(point);
|
|
|
|
+ if (StringUtils.isNotNull(dataSource))
|
|
|
|
+ {
|
|
|
|
+ String dataSourceType = dataSource.value().name();
|
|
|
|
+ DynamicDataSourceContextHolder.setDataSourceType(dataSourceType);
|
|
|
|
+ } else {
|
|
|
|
+ logger.warn("=== 未找到DataSource注解 ===");
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
@After("dsPointCut()")
|
|
@After("dsPointCut()")
|
|
- public void after() {
|
|
|
|
|
|
+ public void after()
|
|
|
|
+ {
|
|
DynamicDataSourceContextHolder.clearDataSourceType();
|
|
DynamicDataSourceContextHolder.clearDataSourceType();
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 获取需要切换的数据源
|
|
|
|
+ */
|
|
|
|
+ public DataSource getDataSource(JoinPoint point)
|
|
|
|
+ {
|
|
|
|
+ MethodSignature signature = (MethodSignature) point.getSignature();
|
|
|
|
+
|
|
|
|
+ DataSource dataSource = AnnotationUtils.findAnnotation(signature.getMethod(), DataSource.class);
|
|
|
|
+ if (Objects.nonNull(dataSource))
|
|
|
|
+ {
|
|
|
|
+ return dataSource;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ dataSource = AnnotationUtils.findAnnotation(signature.getDeclaringType(), DataSource.class);
|
|
|
|
+ if (Objects.nonNull(dataSource))
|
|
|
|
+ {
|
|
|
|
+ return dataSource;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ logger.warn("=== 未找到任何DataSource注解 ===");
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
}
|
|
}
|