|
@@ -1,13 +1,12 @@
|
|
|
package org.jeecg.modules.dataSourceSwitch.aspect;
|
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
-import org.aspectj.lang.JoinPoint;
|
|
|
+import org.aspectj.lang.ProceedingJoinPoint;
|
|
|
+import org.aspectj.lang.annotation.Around;
|
|
|
import org.aspectj.lang.annotation.Aspect;
|
|
|
-import org.aspectj.lang.annotation.Before;
|
|
|
import org.aspectj.lang.annotation.Pointcut;
|
|
|
import org.aspectj.lang.reflect.MethodSignature;
|
|
|
import org.jeecg.modules.dataSourceSwitch.annotation.TargetDataSource;
|
|
|
-import org.jeecg.modules.dataSourceSwitch.entity.SwitchDataSource;
|
|
|
import org.jeecg.modules.dataSourceSwitch.manage.DataSourceManagement;
|
|
|
import org.jeecg.modules.dataSourceSwitch.service.ISwitchDataSourceService;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
@@ -27,38 +26,32 @@ public class TargetDataSourceAspect {
|
|
|
public void dsPointCut() {
|
|
|
}
|
|
|
|
|
|
- @Before("dsPointCut()")
|
|
|
- public void around(JoinPoint point) throws Throwable {
|
|
|
+ @Around("dsPointCut()")
|
|
|
+ public Object around(ProceedingJoinPoint point) throws Throwable {
|
|
|
|
|
|
- DataSourceManagement.flag.set("MASTER");
|
|
|
+ String dataSourceType = DataSourceManagement.getDataSourceType();
|
|
|
+ log.info("原数据源为:"+dataSourceType);
|
|
|
+// DataSourceManagement.setDataSourceType("MASTER");
|
|
|
Integer type = switchDataSourceService.getType();
|
|
|
|
|
|
TargetDataSource annotation = null;
|
|
|
- Class<? extends Object> target = point.getTarget().getClass();
|
|
|
- if(target.isAnnotationPresent(TargetDataSource.class)){
|
|
|
- // 判断类上是否标注着注解
|
|
|
- annotation = target.getAnnotation(TargetDataSource.class);
|
|
|
- log.info("类上标注了注解");
|
|
|
+ Class<? extends Object> target = point.getTarget().getClass();//获得当前访问的class
|
|
|
|
|
|
- if ("SLAVE".equals(annotation.value().name())){
|
|
|
- if(type != null && type == 1){
|
|
|
- DataSourceManagement.flag.set(annotation.value().name());
|
|
|
- log.info("type == 1==========================="+type+annotation.value().name()+"========================");
|
|
|
- }
|
|
|
+ if(target.isAnnotationPresent(TargetDataSource.class)){ // 判断是否存在@注解
|
|
|
+ annotation = target.getAnnotation(TargetDataSource.class);// 判断类上是否标注着注解
|
|
|
+ log.info("类上标注了注解,type="+type);
|
|
|
+
|
|
|
+ if ("SLAVE".equals(annotation.value().name()) && type != null && type == 1){
|
|
|
+ DataSourceManagement.setDataSourceType(annotation.value().name());
|
|
|
}
|
|
|
- log.info(annotation.value().name()+type);
|
|
|
}else{
|
|
|
- Method method = ((MethodSignature) point.getSignature()).getMethod();
|
|
|
- if(method.isAnnotationPresent(TargetDataSource.class)){
|
|
|
- // 判断方法上是否标注着注解,如果类和方法上都没有标注,则报错
|
|
|
+ Method method = ((MethodSignature) point.getSignature()).getMethod();// 得到访问的方法对象
|
|
|
+ if(method.isAnnotationPresent(TargetDataSource.class)){// 判断方法上是否标注着注解,如果类和方法上都没有标注,则报错
|
|
|
annotation = method.getAnnotation(TargetDataSource.class);
|
|
|
- log.info("方法上标注了注解");
|
|
|
+ log.info("方法上标注了注解,type="+type);
|
|
|
|
|
|
- if ("SLAVE".equals(annotation.value().name())){
|
|
|
- if(type != null && type == 1){
|
|
|
- DataSourceManagement.flag.set(annotation.value().name());
|
|
|
- log.info("type == 1==========================="+type+annotation.value().name()+"========================");
|
|
|
- }
|
|
|
+ if ("SLAVE".equals(annotation.value().name()) && type != null && type == 1){
|
|
|
+ DataSourceManagement.setDataSourceType(annotation.value().name());// 取出注解中的数据源名
|
|
|
}
|
|
|
log.info(annotation.value().name()+type);
|
|
|
}else{
|
|
@@ -67,27 +60,16 @@ public class TargetDataSourceAspect {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ try
|
|
|
+ {
|
|
|
+ return point.proceed();
|
|
|
+ }
|
|
|
+ finally
|
|
|
+ {
|
|
|
+ // 销毁数据源 在执行方法之后
|
|
|
+ DataSourceManagement.clearDataSourceType();
|
|
|
+ }
|
|
|
|
|
|
-
|
|
|
-// if ("SLAVE".equals(annotation.value().name())){
|
|
|
-// if(type != null && type == 1){
|
|
|
-// DataSourceManagement.flag.set(annotation.value().name());
|
|
|
-// System.out.println("type == 1==========================="+type+annotation.value().name()+"========================");
|
|
|
-// }
|
|
|
-// }
|
|
|
-// System.out.println(annotation.value().name()+type);
|
|
|
-
|
|
|
-// // 切换数据源 0主数据源,1其他数据源
|
|
|
-// Integer type = switchDataSourceService.getType();
|
|
|
-//// SwitchDataSource switchDataSource = switchDataSourceService.getById(1);
|
|
|
-//// Integer type = Integer.valueOf(switchDataSource.getType());
|
|
|
-// if (type != null && type == 0) {
|
|
|
-// DataSourceManagement.flag.set("MASTER");
|
|
|
-// System.out.println("type == 0==========================="+type+"MASTER========================");
|
|
|
-// } else if (type != null && type == 1) {
|
|
|
-// DataSourceManagement.flag.set(annotation.value().name());
|
|
|
-// System.out.println("type == 1==========================="+type+annotation.value().name()+"========================");
|
|
|
-// }
|
|
|
}
|
|
|
|
|
|
}
|