监听Spring Boot的SpringApplication events

META-INF/spring.factories中添加键值对

org.springframework.context.ApplicationListener=com.example.project.MyListener

其中 MyListener实现org.springframework.context.ApplicationListener接口。

事件触发时机

1. An ApplicationStartedEvent is sent at the start of a run, but before any processing except the
registration of listeners and initializers.
2. An ApplicationEnvironmentPreparedEvent is sent when the Environment to be used in the
context is known, but before the context is created.
3. An ApplicationPreparedEvent is sent just before the refresh is started, but after bean definitions
have been loaded.
4. An ApplicationReadyEvent is sent after the refresh and any related callbacks have been
processed to indicate the application is ready to service requests.
5. An ApplicationFailedEvent is sent if there is an exception on startup.

注:此事件非Spring容器内事件。

参考:http://docs.spring.io/spring-boot/docs/1.4.1.RELEASE/reference/htmlsingle/#boot-features-application-events-and-listeners

转载于:https://my.oschina.net/u/1030459/blog/761808

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot 中,可以使用 Redis 的 Keyspace Notification 功能和 Spring Data Redis 提供的监听器(EventListener)来监控 Redis 中的键删除事件。具体实现步骤如下: 1. 在 Redis 配置文件中开启 Keyspace Notification 功能,设置 notify-keyspace-events 选项为 "Kd",其中 "K" 表示键空间事件,"d" 表示删除事件。例如: ``` notify-keyspace-events Kd ``` 2. 创建一个 RedisKeyDeletedListener 类,实现 RedisKeyExpiredEvent 事件的处理方法: ```java @Component public class RedisKeyDeletedListener implements ApplicationListener<RedisKeyExpiredEvent> { @Override public void onApplicationEvent(RedisKeyExpiredEvent event) { String key = event.getSource().toString(); System.out.println("Key deleted: " + key); } } ``` 3. 在 RedisTemplate 对象上注册监听器: ```java @Configuration public class RedisConfig { @Autowired private RedisKeyDeletedListener redisKeyDeletedListener; @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(redisKeyDeletedListener)); return template; } } ``` 在这个例子中,我们使用 RedisKeyExpiredEvent 事件监听 Redis 中的键删除事件。当 Redis 中的键被删除时,会触发 RedisKeyExpiredEvent 事件,事件源是被删除的键名。在 RedisKeyDeletedListener 中,我们实现 onApplicationEvent 方法,打印出被删除的键名。 在 RedisTemplate 注册监听器的过程中,我们使用了 MessageListenerAdapter 类将 RedisKeyDeletedListener 转化为 MessageListener 对象,然后订阅 Redis 的键事件频道。当 Redis 中有键事件发生时,MessageListenerAdapter 会将事件转发给 RedisKeyDeletedListener 处理。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值