Commit 41799c10f0c72bfc91e5b79f3e4df6e585cc5715
1 parent
25b114aa
修改数据源类型优先级,先根据方法,再根据类
Showing
2 changed files
with
9 additions
and
11 deletions
ruoyi/src/main/java/com/ruoyi/framework/aspectj/DataSourceAspect.java
| 1 | package com.ruoyi.framework.aspectj; | 1 | package com.ruoyi.framework.aspectj; |
| 2 | 2 | ||
| 3 | -import java.lang.reflect.Method; | 3 | +import java.util.Objects; |
| 4 | import org.aspectj.lang.ProceedingJoinPoint; | 4 | import org.aspectj.lang.ProceedingJoinPoint; |
| 5 | import org.aspectj.lang.annotation.Around; | 5 | import org.aspectj.lang.annotation.Around; |
| 6 | import org.aspectj.lang.annotation.Aspect; | 6 | import org.aspectj.lang.annotation.Aspect; |
| @@ -8,6 +8,7 @@ import org.aspectj.lang.annotation.Pointcut; | @@ -8,6 +8,7 @@ import org.aspectj.lang.annotation.Pointcut; | ||
| 8 | import org.aspectj.lang.reflect.MethodSignature; | 8 | import org.aspectj.lang.reflect.MethodSignature; |
| 9 | import org.slf4j.Logger; | 9 | import org.slf4j.Logger; |
| 10 | import org.slf4j.LoggerFactory; | 10 | import org.slf4j.LoggerFactory; |
| 11 | +import org.springframework.core.annotation.AnnotationUtils; | ||
| 11 | import org.springframework.core.annotation.Order; | 12 | import org.springframework.core.annotation.Order; |
| 12 | import org.springframework.stereotype.Component; | 13 | import org.springframework.stereotype.Component; |
| 13 | import com.ruoyi.common.utils.StringUtils; | 14 | import com.ruoyi.common.utils.StringUtils; |
| @@ -60,17 +61,12 @@ public class DataSourceAspect | @@ -60,17 +61,12 @@ public class DataSourceAspect | ||
| 60 | public DataSource getDataSource(ProceedingJoinPoint point) | 61 | public DataSource getDataSource(ProceedingJoinPoint point) |
| 61 | { | 62 | { |
| 62 | MethodSignature signature = (MethodSignature) point.getSignature(); | 63 | MethodSignature signature = (MethodSignature) point.getSignature(); |
| 63 | - Class<? extends Object> targetClass = point.getTarget().getClass(); | ||
| 64 | - DataSource targetDataSource = targetClass.getAnnotation(DataSource.class); | ||
| 65 | - if (StringUtils.isNotNull(targetDataSource)) | 64 | + DataSource dataSource = AnnotationUtils.findAnnotation(signature.getMethod(), DataSource.class); |
| 65 | + if (Objects.nonNull(dataSource)) | ||
| 66 | { | 66 | { |
| 67 | - return targetDataSource; | ||
| 68 | - } | ||
| 69 | - else | ||
| 70 | - { | ||
| 71 | - Method method = signature.getMethod(); | ||
| 72 | - DataSource dataSource = method.getAnnotation(DataSource.class); | ||
| 73 | return dataSource; | 67 | return dataSource; |
| 74 | } | 68 | } |
| 69 | + | ||
| 70 | + return AnnotationUtils.findAnnotation(signature.getDeclaringType(), DataSource.class); | ||
| 75 | } | 71 | } |
| 76 | } | 72 | } |
ruoyi/src/main/java/com/ruoyi/framework/aspectj/lang/annotation/DataSource.java
| @@ -10,7 +10,9 @@ import com.ruoyi.framework.aspectj.lang.enums.DataSourceType; | @@ -10,7 +10,9 @@ import com.ruoyi.framework.aspectj.lang.enums.DataSourceType; | ||
| 10 | 10 | ||
| 11 | /** | 11 | /** |
| 12 | * 自定义多数据源切换注解 | 12 | * 自定义多数据源切换注解 |
| 13 | - * | 13 | + * |
| 14 | + * 优先级:先方法,后类,如果方法覆盖了类上的数据源类型,以方法的为准,否则以类上的为准 | ||
| 15 | + * | ||
| 14 | * @author ruoyi | 16 | * @author ruoyi |
| 15 | */ | 17 | */ |
| 16 | @Target({ ElementType.METHOD, ElementType.TYPE }) | 18 | @Target({ ElementType.METHOD, ElementType.TYPE }) |