流程不重复发起调整

This commit is contained in:
dengwc 2024-03-29 14:42:35 +08:00
parent 42eafa33d2
commit 7b94273155
1 changed files with 26 additions and 11 deletions

View File

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