aop根据目标方法获取拦截器逻辑加入缓存优化,避免每次调用都要重复计算

This commit is contained in:
aoshiguchen
2022-06-27 15:13:57 +08:00
parent 70ee451c60
commit a57269db22
@@ -39,6 +39,7 @@ import java.util.*;
public class InterceptorFactory {
private static final Cache<Class<? extends Interceptor>, Interceptor> interceptorCache = new MemoryCache<>();
private static final List<Interceptor> globalInterceptorList = Collections.synchronizedList(new ArrayList<>());
private static final Map<Method, List<Interceptor>> methodInterceptorListMap = new HashMap<>();
static {
registerGlobalInterceptor(InnerGlobalInterceptor.class);
@@ -59,15 +60,18 @@ public class InterceptorFactory {
}
public static List<Interceptor> getListByTargetMethod(Method targetMethod) {
if (null == targetMethod) {
return null;
}
Assert.notNull(targetMethod, "目标方法不能为空!");
List<Interceptor> interceptors = new ArrayList<>();
interceptors.addAll(globalInterceptorList);
addInterceptorByAnnotation(interceptors, targetMethod.getDeclaringClass().getAnnotation(Intercept.class));
addInterceptorByAnnotation(interceptors, targetMethod.getAnnotation(Intercept.class));
return interceptors;
return LockUtil.doubleCheckProcess(() -> !methodInterceptorListMap.containsKey(targetMethod),
targetMethod,
() -> {
List<Interceptor> interceptors = new ArrayList<>();
interceptors.addAll(globalInterceptorList);
addInterceptorByAnnotation(interceptors, targetMethod.getDeclaringClass().getAnnotation(Intercept.class));
addInterceptorByAnnotation(interceptors, targetMethod.getAnnotation(Intercept.class));
methodInterceptorListMap.put(targetMethod, interceptors);
},
() -> methodInterceptorListMap.get(targetMethod));
}
private static void addInterceptorByAnnotation(List<Interceptor> interceptors, Intercept intercept) {