mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 00:58:24 +08:00
feat: 缓存方法对应的数据源。
This commit is contained in:
parent
6c74c9bdb1
commit
f05ab3e23e
@ -22,6 +22,12 @@ import com.mybatisflex.core.datasource.DataSourceKey;
|
||||
import com.mybatisflex.core.util.StringUtil;
|
||||
import org.aopalliance.intercept.MethodInterceptor;
|
||||
import org.aopalliance.intercept.MethodInvocation;
|
||||
import org.springframework.core.MethodClassKey;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
* 多数据源切换拦截器。
|
||||
@ -39,7 +45,7 @@ public class DataSourceInterceptor implements MethodInterceptor {
|
||||
return invocation.proceed();
|
||||
}
|
||||
|
||||
dsKey = determineDataSourceKey(invocation);
|
||||
dsKey = findDataSourceKey(invocation.getMethod(), invocation.getThis());
|
||||
if (StringUtil.isBlank(dsKey)) {
|
||||
return invocation.proceed();
|
||||
}
|
||||
@ -52,19 +58,34 @@ public class DataSourceInterceptor implements MethodInterceptor {
|
||||
}
|
||||
}
|
||||
|
||||
private String determineDataSourceKey(MethodInvocation invocation) {
|
||||
/**
|
||||
* 缓存方法对应的数据源。
|
||||
*/
|
||||
private final Map<Object, String> dsCache = new ConcurrentHashMap<>();
|
||||
|
||||
private String findDataSourceKey(Method method, Object targetObject) {
|
||||
Object cacheKey = new MethodClassKey(method, targetObject.getClass());
|
||||
String dsKey = this.dsCache.get(cacheKey);
|
||||
if (dsKey == null) {
|
||||
dsKey = determineDataSourceKey(method, targetObject);
|
||||
if (dsKey == null) {
|
||||
dsKey = "";
|
||||
}
|
||||
this.dsCache.put(cacheKey, dsKey);
|
||||
}
|
||||
return dsKey;
|
||||
}
|
||||
|
||||
private String determineDataSourceKey(Method method, Object targetObject) {
|
||||
|
||||
// 方法上定义有 UseDataSource 注解
|
||||
UseDataSource annotation = invocation.getMethod().getAnnotation(UseDataSource.class);
|
||||
UseDataSource annotation = method.getAnnotation(UseDataSource.class);
|
||||
if (annotation != null) {
|
||||
return annotation.value();
|
||||
}
|
||||
|
||||
Object target = invocation.getThis();
|
||||
|
||||
if (target != null) {
|
||||
// 类上定义有 UseDataSource 注解
|
||||
Class<?> targetClass = target.getClass();
|
||||
Class<?> targetClass = targetObject.getClass();
|
||||
annotation = targetClass.getAnnotation(UseDataSource.class);
|
||||
if (annotation != null) {
|
||||
return annotation.value();
|
||||
@ -78,8 +99,6 @@ public class DataSourceInterceptor implements MethodInterceptor {
|
||||
return annotation.value();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user