一篇文章搞懂Spring Boot事件监听机制原理

一、前言

事件监听机制使用23种设计模式中的“观察者模式”,事件监听机制器的核心是广播器org.springframework.context.event.SimpleApplicationEventMulticaster

二、事件监听器注册过程

1.ApplicationListener注册方式

  • SPI方式注册ApplicationListener
  • 静态listener
  • ApplicationListener as a spring bean
  • @EventListener process as a spring bean

2.SimpleApplicationEventMulticaster的初始化

=> SpringApplication run

org.springframework.boot.SpringApplication#run(java.lang.Class<?>, java.lang.String...)

=> SpringApplication run

org.springframework.boot.SpringApplication#run(java.lang.String...)

=> 通过Spring SPI读取sping.factories中配置项Run Listeners(spring-boot) org.springframework.boot.SpringApplicationRunListener=org.springframework.boot.context.event.EventPublishingRunListener

org.springframework.boot.SpringApplication#getRunListeners

=> EventPublishingRunListener初始化

org.springframework.boot.context.event.EventPublishingRunListener#EventPublishingRunListener

=> 广播器SimpleApplicationEventMulticaster初始化

org.springframework.boot.context.event.EventPublishingRunListener#initialMulticaster

3.SPI方式注册ApplicationListener

=> SpringApplication run

org.springframework.boot.SpringApplication#run(java.lang.Class<?>, java.lang.String...)

=> SpringApplication 初始化

org.springframework.boot.SpringApplication#SpringApplication(org.springframework.core.io.ResourceLoader, java.lang.Class<?>...)

=> 通过Spring SPI读取sping.factories中配置项Application Listeners(spring-boot 和 spring-boot-autoconfigure)

setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));


=> SimpleApplicationEventMulticaster静态监听器注册

org.springframework.boot.context.event.EventPublishingRunListener#EventPublishingRunListener

  • 示例
# Application Listeners
org.springframework.context.ApplicationListener=\
com.example.demo.listener.MessageEventListener

4.静态listener和ApplicationListener as a spring bean

=> SpringApplication run

org.springframework.boot.SpringApplication#run(java.lang.Class<?>, java.lang.String...)

=> SpringApplication run

org.springframework.boot.SpringApplication#run(java.lang.String...)

=> refreshContext

org.springframework.boot.SpringApplication#refreshContext

=> refresh

org.springframework.context.support.AbstractApplicationContext#refresh

=> registerListeners 注册静态listener和ApplicationListener Bean

org.springframework.context.support.AbstractApplicationContext#registerListeners

5.@EventListener process as a spring bean

=> SpringApplication run

org.springframework.boot.SpringApplication#run(java.lang.Class<?>, java.lang.String...)

=> SpringApplication run

org.springframework.boot.SpringApplication#run(java.lang.String...)

=> 创建容器

org.springframework.boot.SpringApplication#createApplicationContext

=> 注册直接配置processer Bean

org.springframework.context.annotation.AnnotationConfigUtils#registerAnnotationConfigProcessors(org.springframework.beans.factory.support.BeanDefinitionRegistry, java.lang.Object)

=> 单例实例化完成后

org.springframework.context.event.EventListenerMethodProcessor#afterSingletonsInstantiated

=>

org.springframework.context.event.EventListenerMethodProcessor#processBean

三、事件发布

1.示例

@Service("eventService")
public class EventServiceImpl implements EventService, ApplicationEventPublisherAware {

    private ApplicationEventPublisher applicationEventPublisher;

    @Override
    public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
        this.applicationEventPublisher = applicationEventPublisher;
    }


    @Override
    public void publish() {
        applicationEventPublisher.publishEvent(new MessageApplicationEvent("hello"));
    }
}

2.ApplicationListener接口方式事件发布过程

  • 直接或者间接实现ApplicationListener接口

直接实现ApplicationListener接口需要指定ApplicationEvent类型,仅可以监听一种ApplicationEvent类型;实现SmartApplicationListener(间接实现ApplicationListener接口)可以监听多种ApplicationEvent类型。

=> 发布事件

org.springframework.context.support.AbstractApplicationContext#publishEvent(org.springframework.context.ApplicationEvent)

=> 事件广播器

org.springframework.context.event.SimpleApplicationEventMulticaster#multicastEvent(org.springframework.context.ApplicationEvent, org.springframework.core.ResolvableType)

=> 调用(默认直接调用即与调用者使用同一个线程,因此会阻塞调用者)

org.springframework.context.event.SimpleApplicationEventMulticaster#invokeListener

=> 调用

org.springframework.context.event.SimpleApplicationEventMulticaster#doInvokeListener

3.@EventListener方式事件发布过程

TransactionalEventListener为例

同ApplicationListener接口方式事件发布过程方式


=> 监听适配器

org.springframework.transaction.event.TransactionalApplicationListenerMethodAdapter

=> 监听方法适配器

org.springframework.context.event.ApplicationListenerMethodAdapter#processEvent

参考:

Springboot事件监听机制详解

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Spring Boot 中使用 Redis 监听删除事件,可以结合使用 RedisTemplate 和 Spring Data Redis 提供的监听器(EventListener),具体实现步骤如下: 1. 创建一个 RedisTemplate 对象,用于操作 Redis 数据库: ```java @Configuration public class RedisConfig { @Bean public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) { RedisTemplate<String, Object> template = new RedisTemplate<>(); template.setConnectionFactory(factory); template.setKeySerializer(new StringRedisSerializer()); template.setValueSerializer(new GenericJackson2JsonRedisSerializer()); template.afterPropertiesSet(); return template; } } ``` 2. 创建一个 RedisKeyExpirationListener 类,实现 RedisKeyExpiredEvent 事件的处理方法: ```java @Component public class RedisKeyExpirationListener implements ApplicationListener<RedisKeyExpiredEvent> { @Override public void onApplicationEvent(RedisKeyExpiredEvent event) { String key = event.getSource().toString(); System.out.println("Key expired: " + key); } } ``` 3. 在 RedisTemplate 对象上注册监听器: ```java @Configuration public class RedisConfig { @Autowired private RedisKeyExpirationListener redisKeyExpirationListener; @Bean public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) { RedisTemplate<String, Object> template = new RedisTemplate<>(); template.setConnectionFactory(factory); template.setKeySerializer(new StringRedisSerializer()); template.setValueSerializer(new GenericJackson2JsonRedisSerializer()); template.afterPropertiesSet(); // 注册监听器 template.setEnableTransactionSupport(true); template.getConnectionFactory().getConnection().subscribe(new MessageListenerAdapter(redisKeyExpirationListener)); return template; } } ``` 在这个例子中,我们使用 RedisKeyExpiredEvent 事件监听 Redis 中的键过期事件。当 Redis 中的键过期时,会触发 RedisKeyExpiredEvent 事件,事件源是被删除的键名。在 RedisKeyExpirationListener 中,我们实现 onApplicationEvent 方法,打印出被删除的键名。 在 RedisTemplate 注册监听器的过程中,我们使用了 MessageListenerAdapter 类将 RedisKeyExpirationListener 转化为 MessageListener 对象,然后订阅 Redis 的键事件频道。当 Redis 中有键事件发生时,MessageListenerAdapter 会将事件转发给 RedisKeyExpirationListener 处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

搬山境KL攻城狮

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值