Browse Source

!161 fix 类上数据源类型获取不到问题,修改优先级,先根据方法,再根据类

Merge pull request !161 from sdvdxl/master
pull/161/MERGE
若依 5 years ago committed by Gitee
parent
commit
f24da2fc98
  1. 2
      ruoyi-common/src/main/java/com/ruoyi/common/annotation/DataSource.java
  2. 19
      ruoyi-framework/src/main/java/com/ruoyi/framework/aspectj/DataSourceAspect.java

2
ruoyi-common/src/main/java/com/ruoyi/common/annotation/DataSource.java

@ -10,6 +10,8 @@ import com.ruoyi.common.enums.DataSourceType;
/**
* 自定义多数据源切换注解
*
* 优先级 先方法后类如果方法覆盖了类上的数据源类型以方法的为准否则以类上的为准
*
* @author ruoyi
*/

19
ruoyi-framework/src/main/java/com/ruoyi/framework/aspectj/DataSourceAspect.java

@ -1,6 +1,7 @@
package com.ruoyi.framework.aspectj;
import java.lang.reflect.Method;
import java.util.Objects;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
@ -8,6 +9,7 @@ import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import com.ruoyi.common.annotation.DataSource;
@ -16,7 +18,7 @@ import com.ruoyi.common.utils.StringUtils;
/**
* 多数据源处理
*
*
* @author ruoyi
*/
@Aspect
@ -60,17 +62,14 @@ public class DataSourceAspect
public DataSource getDataSource(ProceedingJoinPoint point)
{
MethodSignature signature = (MethodSignature) point.getSignature();
Class<? extends Object> targetClass = point.getTarget().getClass();
DataSource targetDataSource = targetClass.getAnnotation(DataSource.class);
if (StringUtils.isNotNull(targetDataSource))
// point.getTarget().getClass(); 这个获取的是动态代理的class,上面获取不到自定义注解
// 先获取方法上的,获取不到啊再从class上找
DataSource dataSource = AnnotationUtils.findAnnotation(signature.getMethod(), DataSource.class);
if (Objects.nonNull(dataSource))
{
return targetDataSource;
}
else
{
Method method = signature.getMethod();
DataSource dataSource = method.getAnnotation(DataSource.class);
return dataSource;
}
return AnnotationUtils.findAnnotation(signature.getDeclaringType(), DataSource.class);
}
}

Loading…
Cancel
Save