多数据源支持类注解(允许继承父类的注解)

master
RuoYi 5 years ago committed by Limy
parent 4fee3dbfa8
commit 9fc4665c1a

@ -1,6 +1,8 @@
package com.ruoyi.common.annotation; package com.ruoyi.common.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType; import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention; import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy; import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; import java.lang.annotation.Target;
@ -11,8 +13,10 @@ import com.ruoyi.common.enums.DataSourceType;
* *
* @author ruoyi * @author ruoyi
*/ */
@Target(ElementType.METHOD) @Target({ ElementType.METHOD, ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface DataSource public @interface DataSource
{ {
/** /**

@ -26,7 +26,8 @@ public class DataSourceAspect
{ {
protected Logger logger = LoggerFactory.getLogger(getClass()); 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() public void dsPointCut()
{ {
@ -35,11 +36,7 @@ public class DataSourceAspect
@Around("dsPointCut()") @Around("dsPointCut()")
public Object around(ProceedingJoinPoint point) throws Throwable public Object around(ProceedingJoinPoint point) throws Throwable
{ {
MethodSignature signature = (MethodSignature) point.getSignature(); DataSource dataSource = getDataSource(point);
Method method = signature.getMethod();
DataSource dataSource = method.getAnnotation(DataSource.class);
if (StringUtils.isNotNull(dataSource)) if (StringUtils.isNotNull(dataSource))
{ {
@ -56,4 +53,24 @@ public class DataSourceAspect
DynamicDataSourceContextHolder.clearDataSourceType(); DynamicDataSourceContextHolder.clearDataSourceType();
} }
} }
/**
*
*/
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))
{
return targetDataSource;
}
else
{
Method method = signature.getMethod();
DataSource dataSource = method.getAnnotation(DataSource.class);
return dataSource;
}
}
} }

Loading…
Cancel
Save