AopUtils.getTargetClass(bean).getMethods() 这段代码用于获取一个对象(bean)的所有公共方法(public methods)。让我来解释一下这段代码:
AopUtils.getTargetClass(bean): 这个方法用于获取指定对象的目标类,即被代理的原始类。在Spring AOP中,当一个类被AOP代理后,可能会丢失原始类的一些信息,比如方法,因此需要使用AopUtils.getTargetClass()来获取原始类。
.getMethods(): 这是Class类中的一个方法,用于获取当前类及其父类中所有的公共方法。公共方法指的是被声明为public的方法。
所以,整体来说,AopUtils.getTargetClass(bean).getMethods() 这段代码的作用就是获取一个对象(可能是一个被Spring AOP代理的对象)的所有公共方法。
package com.cnmyhr.ecch.event.core.runner;标签:methods,new,获取,bean,import,注解,AopUtils,方法,event From: https://www.cnblogs.com/zqlmianshi/p/18155656
import com.cnmyhr.ecch.event.core.annotation.AGG;
import com.cnmyhr.ecch.event.core.annotation.AGGListener;
import com.cnmyhr.ecch.event.core.annotation.AGGs;
import lombok.extern.slf4j.Slf4j;
import org.springframework.aop.support.AopUtils;
import org.springframework.context.ApplicationContext;
import java.lang.reflect.Method;
import java.util.*;
import java.util.concurrent.*;
import java.util.stream.Collectors;
@Slf4j
public class AGGRunnerContext {
/**
* 订单事件监听方法
*/
protected final Map<String, List<AGGRunnerMethod>> eventListeners = new HashMap<>();
/**
* 处理消息监听 64个执行线程+1个调度线程
*/
protected final ThreadPoolExecutor pool = new ThreadPoolExecutor(65, 65,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue());
; // 1个监听线程 + 16个处理线程
public AGGRunnerContext(ApplicationContext applicationContext) {
Arrays.stream(applicationContext.getBeanNamesForAnnotation(AGGListener.class))
.forEach(name -> {
Object bean = applicationContext.getBean(name);
for (Method method : AopUtils.getTargetClass(bean).getMethods()) {
List<AGG> aggs = new ArrayList<>();
AGG event = method.getAnnotation(AGG.class);
if (event != null) {
aggs.add(event);
}
AGGs events = method.getAnnotation(AGGs.class);
if (events != null) {
Arrays.stream(events.value()).forEach(e -> aggs.add(e));
}
aggs.forEach(e -> {
List<AGGRunnerMethod> methods = eventListeners.get(e.event());
if (methods == null) {
methods = new ArrayList<>();
eventListeners.put(e.event(), methods);
}
methods.add(new AGGRunnerMethod().setMethod(method).setBean(bean).setTimeout(e.timeout()).setMsgBotName(e.msgBot()));
});
}
});
List<String> msgs = new ArrayList<>();
// 打印事件监听方法
eventListeners.entrySet()
.forEach(e -> {
String msg = String.format("事件: %s, 监听方法: %s", e.getKey(), e.getValue().stream().map(em -> em.method.getName()).collect(Collectors.joining(",")));
msgs.add(msg);
});
log.info("AGG监听注册完成监听器总数:" + msgs.size());
}
}