mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-08 01:28:24 +08:00
optimize DataSourceInterceptor.java
This commit is contained in:
parent
152159a5fb
commit
91654ec7fd
@ -26,7 +26,6 @@ import org.springframework.core.MethodClassKey;
|
|||||||
|
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Objects;
|
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -45,7 +44,7 @@ public class DataSourceInterceptor implements MethodInterceptor {
|
|||||||
return invocation.proceed();
|
return invocation.proceed();
|
||||||
}
|
}
|
||||||
|
|
||||||
dsKey = findDataSourceKey(invocation.getMethod(), invocation.getThis());
|
dsKey = findDataSourceKey(invocation.getMethod(), invocation.getThis().getClass());
|
||||||
if (StringUtil.isBlank(dsKey)) {
|
if (StringUtil.isBlank(dsKey)) {
|
||||||
return invocation.proceed();
|
return invocation.proceed();
|
||||||
}
|
}
|
||||||
@ -63,20 +62,17 @@ public class DataSourceInterceptor implements MethodInterceptor {
|
|||||||
*/
|
*/
|
||||||
private final Map<Object, String> dsCache = new ConcurrentHashMap<>();
|
private final Map<Object, String> dsCache = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
private String findDataSourceKey(Method method, Object targetObject) {
|
private String findDataSourceKey(Method method, Class<?> targetClass) {
|
||||||
Object cacheKey = new MethodClassKey(method, targetObject.getClass());
|
Object cacheKey = new MethodClassKey(method, targetClass);
|
||||||
String dsKey = this.dsCache.get(cacheKey);
|
String dsKey = this.dsCache.get(cacheKey);
|
||||||
if (dsKey == null) {
|
if (dsKey == null) {
|
||||||
dsKey = determineDataSourceKey(method, targetObject);
|
dsKey = determineDataSourceKey(method, targetClass);
|
||||||
if (dsKey == null) {
|
|
||||||
dsKey = "";
|
|
||||||
}
|
|
||||||
this.dsCache.put(cacheKey, dsKey);
|
this.dsCache.put(cacheKey, dsKey);
|
||||||
}
|
}
|
||||||
return dsKey;
|
return dsKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
private String determineDataSourceKey(Method method, Object targetObject) {
|
private String determineDataSourceKey(Method method, Class<?> targetClass) {
|
||||||
|
|
||||||
// 方法上定义有 UseDataSource 注解
|
// 方法上定义有 UseDataSource 注解
|
||||||
UseDataSource annotation = method.getAnnotation(UseDataSource.class);
|
UseDataSource annotation = method.getAnnotation(UseDataSource.class);
|
||||||
@ -85,7 +81,6 @@ public class DataSourceInterceptor implements MethodInterceptor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 类上定义有 UseDataSource 注解
|
// 类上定义有 UseDataSource 注解
|
||||||
Class<?> targetClass = targetObject.getClass();
|
|
||||||
annotation = targetClass.getAnnotation(UseDataSource.class);
|
annotation = targetClass.getAnnotation(UseDataSource.class);
|
||||||
if (annotation != null) {
|
if (annotation != null) {
|
||||||
return annotation.value();
|
return annotation.value();
|
||||||
@ -100,7 +95,7 @@ public class DataSourceInterceptor implements MethodInterceptor {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user