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); + } } } }