spring的事件监听

最近在看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
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 浅谈Spring事件监听在谈Spring的事件监听之前,让我们先了解一下Spring容器,什么是Applicati...
    faunjoe阅读 872评论 0 0
  • 听到监听这个词,不难理解,一个事物根据另一个事物的变化自发的作出响应,而且每次都作出同样的响应。就像点击按钮一样。...
    ZIxuAN_ae3f阅读 10,821评论 2 4
  • Spring事件监听 1.举例 (1)定义监听器监听的对象BaseFetchDataEvent (2)创建一个测试...
    未名枯草阅读 1,256评论 0 0
  • 1. 什么是事件监听机制 在讲解事件监听机制前,我们先回顾下设计模式中的观察者模式,因为事件监听机制可以说是在典型...
    habit_learning阅读 4,173评论 1 13
  • 久违的晴天,家长会。 家长大会开好到教室时,离放学已经没多少时间了。班主任说已经安排了三个家长分享经验。 放学铃声...
    飘雪儿5阅读 7,577评论 16 22