From 41799c10f0c72bfc91e5b79f3e4df6e585cc5715 Mon Sep 17 00:00:00 2001 From: RuoYi Date: Tue, 19 May 2020 11:00:47 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=95=B0=E6=8D=AE=E6=BA=90?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B=E4=BC=98=E5=85=88=E7=BA=A7=EF=BC=8C=E5=85=88?= =?UTF-8?q?=E6=A0=B9=E6=8D=AE=E6=96=B9=E6=B3=95=EF=BC=8C=E5=86=8D=E6=A0=B9?= =?UTF-8?q?=E6=8D=AE=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../framework/aspectj/DataSourceAspect.java | 16 ++++++---------- .../aspectj/lang/annotation/DataSource.java | 4 +++- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/ruoyi/src/main/java/com/ruoyi/framework/aspectj/DataSourceAspect.java b/ruoyi/src/main/java/com/ruoyi/framework/aspectj/DataSourceAspect.java index 5bdff86b6..bc850732f 100644 --- a/ruoyi/src/main/java/com/ruoyi/framework/aspectj/DataSourceAspect.java +++ b/ruoyi/src/main/java/com/ruoyi/framework/aspectj/DataSourceAspect.java @@ -1,6 +1,6 @@ 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 +8,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.utils.StringUtils; @@ -60,17 +61,12 @@ public class DataSourceAspect public DataSource getDataSource(ProceedingJoinPoint point) { MethodSignature signature = (MethodSignature) point.getSignature(); - Class targetClass = point.getTarget().getClass(); - DataSource targetDataSource = targetClass.getAnnotation(DataSource.class); - if (StringUtils.isNotNull(targetDataSource)) + 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); } } diff --git a/ruoyi/src/main/java/com/ruoyi/framework/aspectj/lang/annotation/DataSource.java b/ruoyi/src/main/java/com/ruoyi/framework/aspectj/lang/annotation/DataSource.java index 4df29ff10..15a1d82eb 100644 --- a/ruoyi/src/main/java/com/ruoyi/framework/aspectj/lang/annotation/DataSource.java +++ b/ruoyi/src/main/java/com/ruoyi/framework/aspectj/lang/annotation/DataSource.java @@ -10,7 +10,9 @@ import com.ruoyi.framework.aspectj.lang.enums.DataSourceType; /** * 自定义多数据源切换注解 - * + * + * 优先级:先方法,后类,如果方法覆盖了类上的数据源类型,以方法的为准,否则以类上的为准 + * * @author ruoyi */ @Target({ ElementType.METHOD, ElementType.TYPE })