流程不重复发起调整

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.dispatch.ResultContainer;
import com.chint.infrastructure.echo_framework.queue.SimpleMessageQueue; import com.chint.infrastructure.echo_framework.queue.SimpleMessageQueue;
import lombok.Data; import lombok.Data;
import org.springframework.aop.framework.AopProxyUtils;
import org.springframework.aop.support.AopUtils;
import org.springframework.beans.BeansException; import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanNameAware; import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.InitializingBean;
@ -59,23 +61,36 @@ public class EventManager implements ApplicationContextAware, BeanNameAware, Ini
String[] allBeanNames = context.getBeanDefinitionNames(); String[] allBeanNames = context.getBeanDefinitionNames();
for (String beanName : allBeanNames) { for (String beanName : allBeanNames) {
if (this.beanName.equals(beanName)) continue; if (this.beanName.equals(beanName)) continue;
Object bean = context.getBean(beanName); Object bean = context.getBean(beanName);//获取的可能是动态代理的类
scanAndRegister(bean); scanAndRegister(bean);
} }
} }
private void scanAndRegister(Object bean) { private void scanAndRegister(Object bean) {
Method[] methods = bean.getClass().getDeclaredMethods(); Method[] methods = null;
for (Method method : methods) { //判断是何种类型的代理,使得动态代理的自定义注解不会失效
if (method.isAnnotationPresent(ListenTo.class)) { if (AopUtils.isJdkDynamicProxy(bean)) {
ListenTo listenTo = method.getAnnotation(ListenTo.class); Object singletonTarget = AopProxyUtils.getSingletonTarget(bean);
MethodContainer container = new MethodContainer(bean, listenTo.order(), method); if (singletonTarget != null) {
listenerMethods.computeIfAbsent(listenTo.command(), k -> new ArrayList<>()).add(container); methods = singletonTarget.getClass().getDeclaredMethods();
} }
if (method.isAnnotationPresent(TransitionTo.class)) { } else if (AopUtils.isCglibProxy(bean)) {
TransitionTo transitionTo = method.getAnnotation(TransitionTo.class); methods = bean.getClass().getSuperclass().getDeclaredMethods();
MethodContainer container = new MethodContainer(bean, transitionTo.order(), method); } else {
transitionMethods.computeIfAbsent(transitionTo.command(), k -> new ArrayList<>()).add(container); 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);
}
} }
} }
} }