|
@@ -2,14 +2,10 @@ 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.customColumns.entity.CustomColumns;
|
|
|
-import org.jeecg.modules.customColumns.service.ICustomColumnsService;
|
|
|
import org.jeecg.modules.dataSourceSwitch.annotation.TargetDataSource;
|
|
|
import org.jeecg.modules.dataSourceSwitch.entity.SwitchDataSource;
|
|
|
import org.jeecg.modules.dataSourceSwitch.manage.DataSourceManagement;
|
|
@@ -18,8 +14,6 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import java.lang.reflect.Method;
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.Map;
|
|
|
|
|
|
@Component
|
|
|
@Aspect
|
|
@@ -36,34 +30,34 @@ public class TargetDataSourceAspect {
|
|
|
@Before("dsPointCut()")
|
|
|
public void around(JoinPoint point) throws Throwable {
|
|
|
|
|
|
-// TargetDataSource annotation = null;
|
|
|
-// Class<? extends Object> target = point.getTarget().getClass();
|
|
|
-// if(target.isAnnotationPresent(TargetDataSource.class)){
|
|
|
-// // 判断类上是否标注着注解
|
|
|
-// annotation = target.getAnnotation(TargetDataSource.class);
|
|
|
-// log.info("类上标注了注解");
|
|
|
-// }else{
|
|
|
-// Method method = ((MethodSignature) point.getSignature()).getMethod();
|
|
|
-// if(method.isAnnotationPresent(TargetDataSource.class)){
|
|
|
-// // 判断方法上是否标注着注解,如果类和方法上都没有标注,则报错
|
|
|
-// annotation = method.getAnnotation(TargetDataSource.class);
|
|
|
-// log.info("方法上标注了注解");
|
|
|
-// }else{
|
|
|
-// throw new RuntimeException("@TargetDataSource注解只能用于类或者方法上, 错误出现在:[" +
|
|
|
-// target.toString() +" " + method.toString() + "];");
|
|
|
-// }
|
|
|
-// }
|
|
|
+ TargetDataSource annotation = null;
|
|
|
+ Class<? extends Object> target = point.getTarget().getClass();
|
|
|
+ if(target.isAnnotationPresent(TargetDataSource.class)){
|
|
|
+ // 判断类上是否标注着注解
|
|
|
+ annotation = target.getAnnotation(TargetDataSource.class);
|
|
|
+ log.info("类上标注了注解");
|
|
|
+ }else{
|
|
|
+ Method method = ((MethodSignature) point.getSignature()).getMethod();
|
|
|
+ if(method.isAnnotationPresent(TargetDataSource.class)){
|
|
|
+ // 判断方法上是否标注着注解,如果类和方法上都没有标注,则报错
|
|
|
+ annotation = method.getAnnotation(TargetDataSource.class);
|
|
|
+ log.info("方法上标注了注解");
|
|
|
+ }else{
|
|
|
+ throw new RuntimeException("@TargetDataSource注解只能用于类或者方法上, 错误出现在:[" +
|
|
|
+ target.toString() +" " + method.toString() + "];");
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
// 切换数据源 0主数据源,1其他数据源
|
|
|
SwitchDataSource switchDataSource = switchDataSourceService.getById(1);
|
|
|
Integer type = Integer.valueOf(switchDataSource.getType());
|
|
|
- System.out.println("==========================="+type+"========================");
|
|
|
if (type != null && type == 0) {
|
|
|
DataSourceManagement.flag.set("MASTER");
|
|
|
+ System.out.println("==========================="+type+"MASTER========================");
|
|
|
} else if (type != null && type == 1) {
|
|
|
- DataSourceManagement.flag.set("SLAVE");
|
|
|
+ DataSourceManagement.flag.set(annotation.value().name());
|
|
|
+ System.out.println("==========================="+type+annotation.value().name()+"========================");
|
|
|
}
|
|
|
-// DataSourceManagement.flag.set(annotation.value().name());
|
|
|
}
|
|
|
|
|
|
}
|