当前位置: 首页 > news >正文

动态添加Spring的事件监听

在实际项目中,有些事件可能需要动态添加监听器,比如从配置中加载

在Spring中好像没有可以指定事件添加监听器方法,都是根据类反射得到监听的事件类型

只能自己实现一下监听器了

public class DynamicSpringEventListener implements GenericApplicationListener {private final ApplicationListener<ApplicationEvent> delegate;@Nullableprivate final ResolvableType declaredEventType;public DynamicSpringEventListener(Class<? extends ApplicationEvent> eventClass, ApplicationListener<ApplicationEvent> delegate) {Assert.notNull(delegate, "Delegate listener must not be null");this.delegate = delegate;this.declaredEventType = ResolvableType.forClass(eventClass);}public void onApplicationEvent(ApplicationEvent event) {this.delegate.onApplicationEvent(event);}public boolean supportsEventType(ResolvableType eventType) {return this.declaredEventType == null || this.declaredEventType.isAssignableFrom(eventType);}public boolean supportsSourceType(@Nullable Class<?> sourceType) {return true;}@Overridepublic int getOrder() {return (this.delegate instanceof Ordered ? ((Ordered) this.delegate).getOrder() : Ordered.LOWEST_PRECEDENCE);}@Overridepublic String getListenerId() {return (this.delegate instanceof SmartApplicationListener ? ((SmartApplicationListener) this.delegate).getListenerId() : "");}
}

使用方法

package com.test;import jakarta.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ApplicationEventMulticaster;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;@Component
public class TestPublisher {@Autowiredprivate ApplicationEventMulticaster eventMulticaster;@Autowiredprivate ApplicationEventPublisher eventPublisher;public void addEventListener(Class<? extends ApplicationEvent> eventClass) {System.err.println("add event for class " + eventClass.getSimpleName());DynamicSpringEventListener eventListener = new DynamicSpringEventListener(eventClass, event -> {System.err.println("on test event");});eventMulticaster.addApplicationListener(eventListener);}}

  根据需要动态添加

    @PostConstructpublic void init() {Class[] events = new Class[] {TestApplicationEvent.class};for (Class clazz : events) {addEventListener(clazz);}}

  

 

http://www.jsqmd.com/news/94996/

相关文章:

  • Maven依赖冲突怎么解决?Jar包冲突排查指南
  • 蜜度携文修大模型亮相人工智能先进技术成果供需对接大会 赋能各行业智能化转型
  • 10 个课堂汇报降重工具,本科生AI查重率优化推荐
  • HunyuanVideo-Foley音效生成延迟优化方案:减少推理时间至毫秒级
  • LLM基础知识,langchainV1.0讲解(一)
  • 25 岁转行晚了?网安工程师手把手带你入门到精通,来得及!
  • 实用指南:ES Module 原理详解
  • 百度指数分析:Qwen3-VL-8B相关搜索趋势上升原因
  • Reactor Core终极指南:掌握高性能异步编程的完整教程
  • xm C语言12
  • Git commit message规范助力Qwen-Image-Edit-2509协作开发
  • Ruby CGI 编程
  • 10 个自考开题演讲稿工具,AI 工具推荐优化写作效率
  • Stable Diffusion 3.5 FP8性能实测:推理速度提升40%背后的黑科技
  • 9 个继续教育课堂汇报工具,降AI率AIGC推荐
  • PostgreSQL JOIN 操作详解
  • 如何快速掌握MTKClient:联发科芯片调试的完整入门指南
  • 9 个开题演讲稿 AI 工具,本科生论文写作推荐
  • 智慧楼宇厕所解决方案实现远程管控
  • 测试文章标题02
  • Bootstrap5 小工具详解
  • ionic 单选框详解
  • tsai 完整训练流程实践指南
  • 利用HunyuanVideo-Foley和GitHub开源生态构建自动化视频后期流水线
  • Zotero文献去重终极指南:智能合并重复条目的完整教程
  • 数据驱动决策:如何利用BI工具挖掘大数据商业价值?
  • 8 个MBA答辩PPT工具,AI降AIGC率推荐
  • BOE(京东方)发布智能体集群 多智能体协同重塑AI+显示新未来
  • HunyuanVideo-Foley实战教程:如何在本地部署并生成高质量视频音效
  • PyTorch Lightning是否适用于Qwen-Image训练复现?