aop优化,支持动态指定拦截器拦截某方法
This commit is contained in:
@@ -31,6 +31,8 @@ import fun.asgc.neutrino.core.util.Assert;
|
||||
import fun.asgc.neutrino.core.util.ClassUtil;
|
||||
import fun.asgc.neutrino.core.util.LockUtil;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author: aoshiguchen
|
||||
@@ -141,7 +143,21 @@ public class Aop {
|
||||
InterceptorFactory.registerGlobalInterceptor(interceptorType);
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册类级别的拦截器
|
||||
* @param targetType
|
||||
* @param interceptorType
|
||||
*/
|
||||
public static void intercept(Class<?> targetType, Class<? extends Interceptor> interceptorType) {
|
||||
InterceptorFactory.registerInterceptor(targetType, interceptorType);
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册方法级别的拦截器
|
||||
* @param targetMethod
|
||||
* @param interceptorType
|
||||
*/
|
||||
public static void intercept(Method targetMethod, Class<? extends Interceptor> interceptorType) {
|
||||
InterceptorFactory.registerInterceptor(targetMethod, interceptorType);
|
||||
}
|
||||
}
|
||||
|
||||
+36
-21
@@ -243,28 +243,26 @@ public class InterceptorFactory {
|
||||
public static List<Interceptor> getListByTargetMethod(Method targetMethod) {
|
||||
Assert.notNull(targetMethod, "目标方法不能为空!");
|
||||
|
||||
return LockUtil.doubleCheckProcessForNoException(() -> !methodInterceptorListMap.containsKey(targetMethod),
|
||||
targetMethod,
|
||||
() -> {
|
||||
List<Interceptor> interceptors = new ArrayList<>();
|
||||
interceptors.addAll(globalInterceptorList);
|
||||
if (classInterceptorListMap.containsKey(targetMethod.getDeclaringClass())) {
|
||||
interceptors.addAll(classInterceptorListMap.get(targetMethod.getDeclaringClass()));
|
||||
}
|
||||
addInterceptorByAnnotation(interceptors, targetMethod.getDeclaringClass().getAnnotation(Intercept.class));
|
||||
addInterceptorByAnnotation(interceptors, targetMethod.getAnnotation(Intercept.class));
|
||||
// 如果被代理方法所属类是一个接口,那么该接口所有继承接口链路上的注解都对该方法生效
|
||||
if (ClassUtil.isInterface(targetMethod.getDeclaringClass())) {
|
||||
List<Class<?>> interfaceList = ReflectUtil.getInterfaceAll(targetMethod.getDeclaringClass());
|
||||
if (CollectionUtil.notEmpty(interfaceList)) {
|
||||
for (Class<?> clazz : interfaceList) {
|
||||
addInterceptorByAnnotation(interceptors, clazz.getAnnotation(Intercept.class));
|
||||
}
|
||||
List<Interceptor> interceptors = new ArrayList<>();
|
||||
interceptors.addAll(globalInterceptorList);
|
||||
if (classInterceptorListMap.containsKey(targetMethod.getDeclaringClass())) {
|
||||
interceptors.addAll(classInterceptorListMap.get(targetMethod.getDeclaringClass()));
|
||||
}
|
||||
addInterceptorByAnnotation(interceptors, targetMethod.getDeclaringClass().getAnnotation(Intercept.class));
|
||||
addInterceptorByAnnotation(interceptors, targetMethod.getAnnotation(Intercept.class));
|
||||
if (CollectionUtil.notEmpty(methodInterceptorListMap.get(targetMethod))) {
|
||||
interceptors.addAll(methodInterceptorListMap.get(targetMethod));
|
||||
}
|
||||
// 如果被代理方法所属类是一个接口,那么该接口所有继承接口链路上的注解都对该方法生效
|
||||
if (ClassUtil.isInterface(targetMethod.getDeclaringClass())) {
|
||||
List<Class<?>> interfaceList = ReflectUtil.getInterfaceAll(targetMethod.getDeclaringClass());
|
||||
if (CollectionUtil.notEmpty(interfaceList)) {
|
||||
for (Class<?> clazz : interfaceList) {
|
||||
addInterceptorByAnnotation(interceptors, clazz.getAnnotation(Intercept.class));
|
||||
}
|
||||
}
|
||||
methodInterceptorListMap.put(targetMethod, interceptors);
|
||||
},
|
||||
() -> methodInterceptorListMap.get(targetMethod));
|
||||
}
|
||||
return interceptors;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -330,7 +328,7 @@ public class InterceptorFactory {
|
||||
*/
|
||||
public static synchronized void registerInterceptor(Class<?> targetType, Class<? extends Interceptor> interceptorType) {
|
||||
Assert.notNull(targetType, "目标类型不能为空不能为空!");
|
||||
Assert.notNull(targetType, "目标类型不能为空不能为空!");
|
||||
Assert.notNull(interceptorType, "拦截器类型不能为空不能为空!");
|
||||
if (!classInterceptorListMap.containsKey(targetType)) {
|
||||
classInterceptorListMap.put(targetType, new ArrayList<>());
|
||||
}
|
||||
@@ -340,6 +338,23 @@ public class InterceptorFactory {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册方法的拦截器
|
||||
* @param targetMethod
|
||||
* @param interceptorType
|
||||
*/
|
||||
public static synchronized void registerInterceptor(Method targetMethod, Class<? extends Interceptor> interceptorType) {
|
||||
Assert.notNull(targetMethod, "目标方法不能为空不能为空!");
|
||||
Assert.notNull(interceptorType, "拦截器类型不能为空不能为空!");
|
||||
if (!methodInterceptorListMap.containsKey(targetMethod)) {
|
||||
methodInterceptorListMap.put(targetMethod, new ArrayList<>());
|
||||
}
|
||||
Interceptor interceptor = get(interceptorType);
|
||||
if (!containsInterceptor(methodInterceptorListMap.get(targetMethod), interceptor)) {
|
||||
methodInterceptorListMap.get(targetMethod).add(interceptor);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 删除指定拦截器
|
||||
|
||||
@@ -21,8 +21,11 @@
|
||||
*/
|
||||
package fun.asgc.neutrino.core.aop;
|
||||
|
||||
import fun.asgc.neutrino.core.util.ReflectUtil;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author: aoshiguchen
|
||||
@@ -35,5 +38,18 @@ public class Test5 {
|
||||
Aop.intercept(Panda.class, TestInterceptor.class);
|
||||
Panda panda = Aop.get(Panda.class);
|
||||
panda.eat();
|
||||
panda.say("hello");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test2() throws Exception {
|
||||
// 只拦截一个方法
|
||||
Method method = ReflectUtil.getMethods(Panda.class).stream().filter(m -> m.getName().equals("say")).findFirst().get();
|
||||
Aop.intercept(method, TestInterceptor.class);
|
||||
|
||||
Panda panda = Aop.get(Panda.class);
|
||||
panda.eat();
|
||||
|
||||
panda.say("hello");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user