|
@@ -17,7 +17,10 @@ import org.springframework.util.StringUtils;
|
|
|
import java.lang.reflect.Method;
|
|
import java.lang.reflect.Method;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- * 子模块默认数据源:按配置包前缀 push 数据源;若目标已标注 {@link DS} 则跳过,由官方 {@code @DS} 切面处理。
|
|
|
|
|
|
|
+ * 子模块默认数据源:按配置包前缀 push 数据源。
|
|
|
|
|
+ * <p>
|
|
|
|
|
+ * 若目标已标注 {@link DS},由本切面直接 push 其 value(不再仅“跳过”依赖官方切面,
|
|
|
|
|
+ * 避免官方 @DS 未织入时落到 primary/master)。
|
|
|
*/
|
|
*/
|
|
|
@Slf4j
|
|
@Slf4j
|
|
|
@Aspect
|
|
@Aspect
|
|
@@ -37,17 +40,17 @@ public class ModuleDefaultDataSourceAspect {
|
|
|
if (!properties.isModuleDefaultEnabled()) {
|
|
if (!properties.isModuleDefaultEnabled()) {
|
|
|
return joinPoint.proceed();
|
|
return joinPoint.proceed();
|
|
|
}
|
|
}
|
|
|
- if (hasDsAnnotation(joinPoint)) {
|
|
|
|
|
- return joinPoint.proceed();
|
|
|
|
|
|
|
+ String datasource = resolveExplicitDs(joinPoint);
|
|
|
|
|
+ if (!StringUtils.hasText(datasource)) {
|
|
|
|
|
+ datasource = resolveDatasource(joinPoint);
|
|
|
}
|
|
}
|
|
|
- String datasource = resolveDatasource(joinPoint);
|
|
|
|
|
if (!StringUtils.hasText(datasource)) {
|
|
if (!StringUtils.hasText(datasource)) {
|
|
|
return joinPoint.proceed();
|
|
return joinPoint.proceed();
|
|
|
}
|
|
}
|
|
|
DynamicDataSourceContextHolder.push(datasource);
|
|
DynamicDataSourceContextHolder.push(datasource);
|
|
|
try {
|
|
try {
|
|
|
if (log.isDebugEnabled()) {
|
|
if (log.isDebugEnabled()) {
|
|
|
- log.debug("module default datasource [{}] -> {}", datasource, joinPoint.getSignature().toShortString());
|
|
|
|
|
|
|
+ log.debug("module datasource [{}] -> {}", datasource, joinPoint.getSignature().toShortString());
|
|
|
}
|
|
}
|
|
|
return joinPoint.proceed();
|
|
return joinPoint.proceed();
|
|
|
} finally {
|
|
} finally {
|
|
@@ -55,17 +58,39 @@ public class ModuleDefaultDataSourceAspect {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- private boolean hasDsAnnotation(ProceedingJoinPoint joinPoint) {
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 方法 / 声明类 / 实际目标类上的 @DS 优先
|
|
|
|
|
+ */
|
|
|
|
|
+ private String resolveExplicitDs(ProceedingJoinPoint joinPoint) {
|
|
|
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
|
|
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
|
|
|
Method method = signature.getMethod();
|
|
Method method = signature.getMethod();
|
|
|
|
|
+ DS ds = AnnotatedElementUtils.findMergedAnnotation(method, DS.class);
|
|
|
|
|
+ if (ds != null && StringUtils.hasText(ds.value())) {
|
|
|
|
|
+ return ds.value();
|
|
|
|
|
+ }
|
|
|
Class<?> declaringType = signature.getDeclaringType();
|
|
Class<?> declaringType = signature.getDeclaringType();
|
|
|
- if (AnnotatedElementUtils.hasAnnotation(method, DS.class)) {
|
|
|
|
|
- return true;
|
|
|
|
|
|
|
+ if (declaringType != null) {
|
|
|
|
|
+ ds = AnnotatedElementUtils.findMergedAnnotation(declaringType, DS.class);
|
|
|
|
|
+ if (ds != null && StringUtils.hasText(ds.value())) {
|
|
|
|
|
+ return ds.value();
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
- if (declaringType != null && AnnotatedElementUtils.hasAnnotation(declaringType, DS.class)) {
|
|
|
|
|
- return true;
|
|
|
|
|
|
|
+ Class<?> targetClass = joinPoint.getTarget() != null ? joinPoint.getTarget().getClass() : null;
|
|
|
|
|
+ if (targetClass != null) {
|
|
|
|
|
+ ds = AnnotatedElementUtils.findMergedAnnotation(targetClass, DS.class);
|
|
|
|
|
+ if (ds != null && StringUtils.hasText(ds.value())) {
|
|
|
|
|
+ return ds.value();
|
|
|
|
|
+ }
|
|
|
|
|
+ // CGLIB 代理取父类
|
|
|
|
|
+ Class<?> superclass = targetClass.getSuperclass();
|
|
|
|
|
+ if (superclass != null && superclass != Object.class) {
|
|
|
|
|
+ ds = AnnotatedElementUtils.findMergedAnnotation(superclass, DS.class);
|
|
|
|
|
+ if (ds != null && StringUtils.hasText(ds.value())) {
|
|
|
|
|
+ return ds.value();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
- return AnnotatedElementUtils.hasAnnotation(joinPoint.getTarget().getClass(), DS.class);
|
|
|
|
|
|
|
+ return null;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|