diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/annotation/DataSource.java b/ruoyi-common/src/main/java/com/ruoyi/common/annotation/DataSource.java index 5a41a2c6..a272a5b7 100644 --- a/ruoyi-common/src/main/java/com/ruoyi/common/annotation/DataSource.java +++ b/ruoyi-common/src/main/java/com/ruoyi/common/annotation/DataSource.java @@ -1,6 +1,8 @@ package com.ruoyi.common.annotation; +import java.lang.annotation.Documented; import java.lang.annotation.ElementType; +import java.lang.annotation.Inherited; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @@ -11,8 +13,10 @@ import com.ruoyi.common.enums.DataSourceType; * * @author ruoyi */ -@Target(ElementType.METHOD) +@Target({ ElementType.METHOD, ElementType.TYPE }) @Retention(RetentionPolicy.RUNTIME) +@Documented +@Inherited public @interface DataSource { /** diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/aspectj/DataSourceAspect.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/aspectj/DataSourceAspect.java index 89d28fac..4f13ddb3 100644 --- a/ruoyi-framework/src/main/java/com/ruoyi/framework/aspectj/DataSourceAspect.java +++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/aspectj/DataSourceAspect.java @@ -26,7 +26,8 @@ public class DataSourceAspect { protected Logger logger = LoggerFactory.getLogger(getClass()); - @Pointcut("@annotation(com.ruoyi.common.annotation.DataSource)") + @Pointcut("@annotation(com.ruoyi.common.annotation.DataSource)" + + "|| @within(com.ruoyi.common.annotation.DataSource)") public void dsPointCut() { @@ -35,11 +36,7 @@ public class DataSourceAspect @Around("dsPointCut()") public Object around(ProceedingJoinPoint point) throws Throwable { - MethodSignature signature = (MethodSignature) point.getSignature(); - - Method method = signature.getMethod(); - - DataSource dataSource = method.getAnnotation(DataSource.class); + DataSource dataSource = getDataSource(point); if (StringUtils.isNotNull(dataSource)) { @@ -56,4 +53,24 @@ public class DataSourceAspect DynamicDataSourceContextHolder.clearDataSourceType(); } } + + /** + * 获取需要切换的数据源 + */ + 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)) + { + return targetDataSource; + } + else + { + Method method = signature.getMethod(); + DataSource dataSource = method.getAnnotation(DataSource.class); + return dataSource; + } + } }