|
@@ -1,14 +1,12 @@
|
|
|
package com.chelvc.framework.database.interceptor;
|
|
|
|
|
|
-import java.util.Collections;
|
|
|
-import java.util.Map;
|
|
|
-import java.util.Set;
|
|
|
+import java.util.List;
|
|
|
|
|
|
+import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSourceAutoConfiguration;
|
|
|
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSourceProperties;
|
|
|
import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
|
|
|
+import com.beust.jcommander.internal.Lists;
|
|
|
import com.chelvc.framework.common.util.ObjectUtils;
|
|
|
-import com.chelvc.framework.common.util.StringUtils;
|
|
|
-import com.google.common.collect.Maps;
|
|
|
import org.aspectj.lang.ProceedingJoinPoint;
|
|
|
import org.aspectj.lang.annotation.Around;
|
|
|
import org.aspectj.lang.annotation.Aspect;
|
|
@@ -25,23 +23,12 @@ import org.springframework.core.annotation.Order;
|
|
|
*/
|
|
|
@Aspect
|
|
|
@Order(Ordered.HIGHEST_PRECEDENCE)
|
|
|
-@ConditionalOnClass(DynamicDataSourceContextHolder.class)
|
|
|
+@ConditionalOnClass(DynamicDataSourceAutoConfiguration.class)
|
|
|
public class DynamicDatasourceInterceptor {
|
|
|
- private final Map<String, String> datasourceContext;
|
|
|
+ private final List<String> routes;
|
|
|
|
|
|
public DynamicDatasourceInterceptor(DynamicDataSourceProperties properties) {
|
|
|
- // 初始化数据源标识集合
|
|
|
- Set<String> keys = properties.getDatasource().keySet();
|
|
|
- if (ObjectUtils.isEmpty(keys)) {
|
|
|
- this.datasourceContext = Collections.emptyMap();
|
|
|
- } else {
|
|
|
- this.datasourceContext = Maps.newHashMapWithExpectedSize(keys.size());
|
|
|
- keys.forEach(value -> {
|
|
|
- for (String key : StringUtils.splitActives(value)) {
|
|
|
- this.datasourceContext.put(key, value);
|
|
|
- }
|
|
|
- });
|
|
|
- }
|
|
|
+ this.routes = Lists.newArrayList(properties.getDatasource().keySet());
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -55,22 +42,21 @@ public class DynamicDatasourceInterceptor {
|
|
|
|
|
|
// 根据目标类对象查找数据源标识
|
|
|
String className = AopProxyUtils.ultimateTargetClass(target).getName();
|
|
|
- Set<Map.Entry<String, String>> routes = this.datasourceContext.entrySet();
|
|
|
- String key = routes.stream().filter(entry -> className.startsWith(entry.getKey())).findAny()
|
|
|
- .map(Map.Entry::getValue).orElse(null);
|
|
|
- if (StringUtils.notEmpty(key)) {
|
|
|
- return key;
|
|
|
+ for (String route : this.routes) {
|
|
|
+ if (className.startsWith(route)) {
|
|
|
+ return route;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
// 根据目标接口对象查找数据源标识
|
|
|
Class<?>[] interfaces = AopProxyUtils.proxiedUserInterfaces(target);
|
|
|
- if (interfaces.length > 0) {
|
|
|
+ if (ObjectUtils.notEmpty(interfaces)) {
|
|
|
for (Class<?> clazz : interfaces) {
|
|
|
String interfaceName = clazz.getName();
|
|
|
- String route = routes.stream().filter(entry -> interfaceName.startsWith(entry.getKey())).findAny()
|
|
|
- .map(Map.Entry::getValue).orElse(null);
|
|
|
- if (StringUtils.notEmpty(route)) {
|
|
|
- return route;
|
|
|
+ for (String route : this.routes) {
|
|
|
+ if (interfaceName.startsWith(route)) {
|
|
|
+ return route;
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|