From 38b0c4ba20b079458991c5d2a95e5e5cece7d4f0 Mon Sep 17 00:00:00 2001 From: aoshiguchen <1052045476@qq.com> Date: Fri, 8 Jul 2022 14:48:16 +0800 Subject: [PATCH] =?UTF-8?q?aop=E4=BC=98=E5=8C=96=EF=BC=8C=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E5=8A=A8=E6=80=81=E6=8C=87=E5=AE=9A=E6=8B=A6=E6=88=AA?= =?UTF-8?q?=E5=99=A8=E6=8B=A6=E6=88=AA=E6=9F=90=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/fun/asgc/neutrino/core/aop/Aop.java | 16 ++++++ .../aop/interceptor/InterceptorFactory.java | 57 ++++++++++++------- .../fun/asgc/neutrino/core/aop/Test5.java | 16 ++++++ 3 files changed, 68 insertions(+), 21 deletions(-) diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/Aop.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/Aop.java index 1b00c3b1..ee47df7d 100644 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/Aop.java +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/Aop.java @@ -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 interceptorType) { InterceptorFactory.registerInterceptor(targetType, interceptorType); } + + /** + * 注册方法级别的拦截器 + * @param targetMethod + * @param interceptorType + */ + public static void intercept(Method targetMethod, Class interceptorType) { + InterceptorFactory.registerInterceptor(targetMethod, interceptorType); + } } diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/interceptor/InterceptorFactory.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/interceptor/InterceptorFactory.java index 7f2630d4..79053b5c 100644 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/interceptor/InterceptorFactory.java +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/interceptor/InterceptorFactory.java @@ -243,28 +243,26 @@ public class InterceptorFactory { public static List getListByTargetMethod(Method targetMethod) { Assert.notNull(targetMethod, "目标方法不能为空!"); - return LockUtil.doubleCheckProcessForNoException(() -> !methodInterceptorListMap.containsKey(targetMethod), - targetMethod, - () -> { - List 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> interfaceList = ReflectUtil.getInterfaceAll(targetMethod.getDeclaringClass()); - if (CollectionUtil.notEmpty(interfaceList)) { - for (Class clazz : interfaceList) { - addInterceptorByAnnotation(interceptors, clazz.getAnnotation(Intercept.class)); - } + List 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> 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 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 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); + } + } + /** * 删除指定拦截器 diff --git a/neutrino-core/src/test/java/fun/asgc/neutrino/core/aop/Test5.java b/neutrino-core/src/test/java/fun/asgc/neutrino/core/aop/Test5.java index 09a18f9b..44a88286 100644 --- a/neutrino-core/src/test/java/fun/asgc/neutrino/core/aop/Test5.java +++ b/neutrino-core/src/test/java/fun/asgc/neutrino/core/aop/Test5.java @@ -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"); } }