最近在看springboot的源码时候,看到里面有很多观察者模式 也就是发布事件的方式进行处理一些逻辑,实现解耦,正好没事就想学习一下spring的事件是怎么做的
image.png
下面就是demo
#事件实体
public class Event extends ApplicationEvent {
public Event(Object source) {
super(source);
}
}
component 是为了将listener 加到ioc容器管理 Async是异步处理的方式可加可不加 Eventlistener 是spring提供的一个注解 如果不加的话 也可以通过下面这种方式
public class Listener implements ApplicationListener<Event> {
@Component
public class Listener {
@Async
@EventListener
public void onApplicationEvent(Event event) {
System.out.println("listener: "+event.getSource().toString());
}
}
下面看一下EventListener这个注解做的什么
image.png
我们点开@see 里面这个接口
image.png
然后在里面找到processBean这个方法
private void processBean(final String beanName, final Class<?> targetType) {
if (!this.nonAnnotatedClasses.contains(targetType) && !isSpringContainerClass(targetType)) {
Map<Method, EventListener> annotatedMethods = null;
try {
annotatedMethods = MethodIntrospector.selectMethods(targetType,
(MethodIntrospector.MetadataLookup<EventListener>) method ->
AnnotatedElementUtils.findMergedAnnotation(method, EventListener.class));
}
catch (Throwable ex) {
// An unresolvable type in a method signature, probably from a lazy bean - let's ignore it.
if (logger.isDebugEnabled()) {
logger.debug("Could not resolve methods for bean with name '" + beanName + "'", ex);
}
}
if (CollectionUtils.isEmpty(annotatedMethods)) {
this.nonAnnotatedClasses.add(targetType);
if (logger.isTraceEnabled()) {
logger.trace("No @EventListener annotations found on bean class: " + targetType.getName());
}
}
else {
// Non-empty set of methods
ConfigurableApplicationContext context = this.applicationContext;
Assert.state(context != null, "No ApplicationContext set");
List<EventListenerFactory> factories = this.eventListenerFactories;
Assert.state(factories != null, "EventListenerFactory List not initialized");
for (Method method : annotatedMethods.keySet()) {
for (EventListenerFactory factory : factories) {
if (factory.supportsMethod(method)) {
Method methodToUse = AopUtils.selectInvocableMethod(method, context.getType(beanName));
# 主要是这个方法
ApplicationListener<?> applicationListener =
factory.createApplicationListener(beanName, targetType, methodToUse);
if (applicationListener instanceof ApplicationListenerMethodAdapter) {
((ApplicationListenerMethodAdapter) applicationListener).init(context, this.evaluator);
}
context.addApplicationListener(applicationListener);
break;
}
}
}
if (logger.isDebugEnabled()) {
logger.debug(annotatedMethods.size() + " @EventListener methods processed on bean '" +
beanName + "': " + annotatedMethods);
}
}
}
}
可以看到 他是先进到afterSingletonsInstantiated这个方法 然后通过beanFactory拿到所有的bean. 在这里面就有名为listener的bean,就是我们之前注入的
在这里面找到EventListener这个注解方法集合,后面就是遍历这个集合,createApplicationListener 创建监听
image.png
String[] beanNames = beanFactory.getBeanNamesForType(Object.class);
image.png
下面看一下ApplicationListener,这个只是看的话是找不到具体实现方式的,不过我们可以从publisEvent 来反推 看他的回调是哪个接口
image.png
关键代码就是下面这两个方法
image.png
image.png
public void multicastEvent(final ApplicationEvent event, @Nullable ResolvableType eventType) {
ResolvableType type = (eventType != null ? eventType : resolveDefaultEventType(event));
for (final ApplicationListener<?> listener : getApplicationListeners(event, type)) {
Executor executor = getTaskExecutor();
if (executor != null) {
executor.execute(() -> invokeListener(listener, event));
}
else {
invokeListener(listener, event);
}
}
}
这里我们就能看到他是跟我你的事件event类型来获取这个事件类型的监听。通过这种方式来发布事件
image.png
后面invokeListener 调用doinvokeListener 最后也是通过aop实现的
image.png
image.png