From 191e3b318020d81cb210b969ab0759f1a1ee13cc Mon Sep 17 00:00:00 2001 From: dengwc Date: Fri, 29 Mar 2024 14:42:35 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B5=81=E7=A8=8B=E4=B8=8D=E9=87=8D=E5=A4=8D?= =?UTF-8?q?=E5=8F=91=E8=B5=B7=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../echo_framework/manager/EventManager.java | 37 +++++++++++++------ 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/chint/infrastructure/echo_framework/manager/EventManager.java b/src/main/java/com/chint/infrastructure/echo_framework/manager/EventManager.java index 4908733e..f73aab18 100644 --- a/src/main/java/com/chint/infrastructure/echo_framework/manager/EventManager.java +++ b/src/main/java/com/chint/infrastructure/echo_framework/manager/EventManager.java @@ -8,6 +8,8 @@ import com.chint.infrastructure.echo_framework.command.EmptyCommand; import com.chint.infrastructure.echo_framework.dispatch.ResultContainer; import com.chint.infrastructure.echo_framework.queue.SimpleMessageQueue; import lombok.Data; +import org.springframework.aop.framework.AopProxyUtils; +import org.springframework.aop.support.AopUtils; import org.springframework.beans.BeansException; import org.springframework.beans.factory.BeanNameAware; import org.springframework.beans.factory.InitializingBean; @@ -59,23 +61,36 @@ public class EventManager implements ApplicationContextAware, BeanNameAware, Ini String[] allBeanNames = context.getBeanDefinitionNames(); for (String beanName : allBeanNames) { if (this.beanName.equals(beanName)) continue; - Object bean = context.getBean(beanName); + Object bean = context.getBean(beanName);//获取的可能是动态代理的类 scanAndRegister(bean); } } private void scanAndRegister(Object bean) { - Method[] methods = bean.getClass().getDeclaredMethods(); - for (Method method : methods) { - if (method.isAnnotationPresent(ListenTo.class)) { - ListenTo listenTo = method.getAnnotation(ListenTo.class); - MethodContainer container = new MethodContainer(bean, listenTo.order(), method); - listenerMethods.computeIfAbsent(listenTo.command(), k -> new ArrayList<>()).add(container); + Method[] methods = null; + //判断是何种类型的代理,使得动态代理的自定义注解不会失效 + if (AopUtils.isJdkDynamicProxy(bean)) { + Object singletonTarget = AopProxyUtils.getSingletonTarget(bean); + if (singletonTarget != null) { + methods = singletonTarget.getClass().getDeclaredMethods(); } - if (method.isAnnotationPresent(TransitionTo.class)) { - TransitionTo transitionTo = method.getAnnotation(TransitionTo.class); - MethodContainer container = new MethodContainer(bean, transitionTo.order(), method); - transitionMethods.computeIfAbsent(transitionTo.command(), k -> new ArrayList<>()).add(container); + } else if (AopUtils.isCglibProxy(bean)) { + methods = bean.getClass().getSuperclass().getDeclaredMethods(); + } else { + methods = bean.getClass().getDeclaredMethods(); + } + if (methods != null) { + for (Method method : methods) { + if (method.isAnnotationPresent(ListenTo.class)) { + ListenTo listenTo = method.getAnnotation(ListenTo.class); + MethodContainer container = new MethodContainer(bean, listenTo.order(), method); + listenerMethods.computeIfAbsent(listenTo.command(), k -> new ArrayList<>()).add(container); + } + if (method.isAnnotationPresent(TransitionTo.class)) { + TransitionTo transitionTo = method.getAnnotation(TransitionTo.class); + MethodContainer container = new MethodContainer(bean, transitionTo.order(), method); + transitionMethods.computeIfAbsent(transitionTo.command(), k -> new ArrayList<>()).add(container); + } } } }