SpringBoot版本:2.0.2.RELEASE
SpringFramework版本:5.0.6.RELEASE
之前的“SpringFramework事件与监听机制”系列文章已经从接口层面上介绍了事件、监听器和发布器。其实使用@EventListener也能达到ApplicationLister接口的效果,根据我们对spring的认识,它们不会轻易放过代码重用的机会,那么@EventListener标注的使用,是否基于SpringFramework事件与监听机制呢?本文旨在探讨@EventListener是如何工作的,所以@EventListener使用上的细节,请在官网或者其实阅读资料查询。
@EventListener的初始化工作
EventListenerMethodProcessor与DefaultEventListenerFactory是两个重要的类,它们在AnnotationConfigUtils#registerAnnotationConfigProcessors方法里被注册到BeanDefinitionRegistry去了。
在SpringBoot程序的程动过程中,在构造AnnotationConfigApplicationContext的时候会调用此方法。它的方法调用链如下: