spring中的事件监听eventListener

spring中的事件监听eventListener,第1张

title: spring中的事件监听eventListener

date: 2017-04-20 14:00:59

tags:

category: spring

官网说明: http://docs.spring.io/spring/docs/2.5.x/reference/beans.html#context-functionality-events

springboot新加入一些内建event:

https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#boot-features-application-events-and-listeners

继承ApplicationEvent或相应子类。

4.2版本之后,不再强制要求继承ApplicationEvent,非ApplicationEvent子类的对象将被包装成PayloadApplicationEvent,其源码中的payload既我们传入的事件对象。

事件的发送者是实现了ApplicationEventPublisher接口的类。在spring中,其实就是你的ApplicationContext。

当你的bean实现ApplicationEventPublisherAware接口时,spring会自动为你注入ApplicationEventPublisher,也就是当前ApplicationContext。其实为了避免繁琐,直接注入即可。

调用applicationContext.publish(yourEvent);既成功发送。

同理事件的监听器,或者说处理者,既spring管理的bean中实现了ApplicationListener的相应类。根据事件的类型和对应listener接受的事件类型参数 ,被发送到相应的listener。

在spring4.2版中,允许在任一bean的对应方法上注解@EventListener来标记该bean实现了listener方法。实现更大程度的解耦。2种实现方式如下:

这里有个进阶使用方式是如果@EventListener标记的方法的返回值不是void,返回的对象将再次作为一个Event被发送。

同时@EventListener也可以和@Async配合使用,使此方法被包裹成任务放入线程池中异步执行。(前提是开启了异步任务池设置 @EnableAsync )

1.Spring事件监听体系包括三个组件:事件、事件监听器,事件广播器。

事件:定义事件类型和事件源,需要继承ApplicationEvent。

事件监听器:用来监听某一类的事件,并且执行具体业务逻辑,需要实现ApplicationListener 接口或者需要用@ListenerEvent(T)注解。好比观察者模式中的观察者。

事件多播器:负责广播通知所有监听器,所有的事件监听器都注册在了事件多播器中。好比观察者模式中的被观察者。Spring容器默认生成的是同步事件多播器。可以自定义事件多播器,定义为异步方式。

创建 AnnotationConfigApplicationContext 的过程中,会执行refresh()中的initApplicationEventMulticaster()方法。该方法先获取bean工厂,然后判断工厂是否包含了beanName 为 applicationEventMulticaster的bean。如果包含了,则获取该bean,赋值给applicationEventMulticaster 属性。如果没有,则创建一个 SimpleApplicationEventMulticaster 对象,并且赋值给 applicationEventMulticaster 。实现了源码如下:

监听器的注册有两种,通过实现 ApplicationListener接口或者添加@EventListener注解。

注册的逻辑实现在refresh()中的registerListeners()方法里面。第一步,先获取当前ApplicationContext中已经添加的 applicationListeners(SpringMVC源码中有用到),遍历添加到多播器中。第二步,获取实现了ApplicationListener接口的listenerBeanNames集合,添加至多播器中。第三步,判断是否有早期事件,如果有则发起广播。

思考一下,上面的代码中第二步为啥添加的是listenerBeanName?

如果监听器是懒加载的话(即有@Lazy 注解)。那么在这个时候创建监听器显然是不对的,这个时候不能创建监听器。所以添加监听器到多播器的具体逻辑放在初始化具体的监听器之后。通过 BeanPostProcessor 的接口实现。具体的实现类是 ApplicationListenerDetector 。这个类是在 refreah()中prepareBeanFactory()方法中添加的。代码如下:

在创建 AnnotationConfigApplicationContext 的构造方法中,会执行org.springframework.context.annotation.AnnotationConfigUtils#registerAnnotationConfigProcessors(org.springframework.beans.factory.support.BeanDefinitionRegistry, java.lang.Object) 方法。这个方法中会添加两个 beanDefs, 代码如下:

EventListenerMethodProcessor:事件监听器的BeanFactory后置处理器,在前期会创建 DefaultEventListenerFactory ,后期在创建好Bean之后,根据 EventListener 属性,调用DefaultEventListenerFactory创建具体的 ApplicationListenerMethodAdapter 。

DefaultEventListenerFactory:监听器的创建工厂,用来创建 ApplicationListenerMethodAdapter 。

EventListenerMethodProcessor 的类继承图如下:

在refreash的invokeBeanFactoryPostProcessors()中会调用 org.springframework.context.event.EventListenerMethodProcessor#postProcessBeanFactory方法,获取EventListenerFactory 类型的 Bean。代码如下:

在 org.springframework.beans.factory.support.DefaultListableBeanFactory#preInstantiateSingletons 方法中,创建完所有的单例Bean 之后,会遍历所有Bean是否实现了 SmartInitializingSingleton 接口。如果实现接口会执行该 Bean 的 afterSingletonsInstantiated() 方法。代码如下:

org.springframework.context.event.EventListenerMethodProcessor#afterSingletonsInstantiated 中会调用私有方法 processBean()进行 ApplicationEventAdatper 的创建。代码如下:

可以通过调用 org.springframework.context.support.AbstractApplicationContext#publishEvent(java.lang.Object, org.springframework.core.ResolvableType) 方法进行事件的调用。代码如下:

SimpleApplicationEventMulticaster 中的 multicasEvent,invokeListener,doInvokeListener 三个方法代码如下:

SpringMVC中就是通过Spring的事件机制进行九大组件的初始化。

监听器定义在FrameworkServlet类中,作为内部类。代码如下:

监听器的添加在org.springframework.web.servlet.FrameworkServlet#configureAndRefreshWebApplicationContext 中进行。通过SourceFilteringListener进行包装。添加代码如下:

在refresh中的registerListeners方法进行添加,代码如下:

在refresh中的finishRefresh()方法中,会调用publishEvnet(new ContextRefreshedEvent(this))发布事件。进行多播器广播,代码如下

最终会调到FrameworkServlet.this.onApplicationEvent(event)。


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/bake/11603916.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-17
下一篇2023-05-17

发表评论

登录后才能评论

评论列表(0条)

    保存