【Spring Boot实战与进阶】自定义事件及监听

简介: 自定义事件及监听
Spring Boot是很优秀的框架,它的出现简化了新Spring应用的初始搭建以及开发过程,大大减少了代码量,目前已被大多数企业认可和使用。这个专栏将对Spring Boot框架从浅入深,从实战到进阶,不但我们要懂得如何去使用,还要去剖析框架源码,学习其优秀的设计思想。

汇总目录链接:【Spring Boot实战与进阶】学习目录

  这里的自定义事件及监听,其实早在Spring框架就有完善的事件监听机制。Spring的事件为Bean与Bean之间的消息通信提供了支持。当一个Bean处理完任务后,希望另一个Bean知道并能做相应的处理,这时就需要让另一个Bean监听当前Bean的所发送的事件。

  Spring框架中实现监听事件的流程
(1)自定义事件,继承ApplicationEvent抽象类
(2)定义事件监听器,实现ApplicationListener接口
(3)使用容器中发布事件

示例一

1、自定义事件

public class MyApplicationEvent extends ApplicationEvent {
    public MyApplicationEvent(Object source) {
        super(source);
    }
}
AI 代码解读

2、定义事件监听器

public class MyApplicationListener implements ApplicationListener<MyApplicationEvent> {

    @Override
    public void onApplicationEvent(MyApplicationEvent event) {
        System.out.println("接受到了事件:"+event.getClass());
        System.out.println("接受到了事件:"+event.getSource());
    }
}
AI 代码解读

3、使用容器中发布事件

@SpringBootApplication
public class EventDemoApplication {

    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(EventDemoApplication.class);
        //1 添加监听事件
        app.addListeners(new MyApplicationListener());
        ConfigurableApplicationContext context = app.run(args);
        // 发布事件
        context.publishEvent(new MyApplicationEvent(new Object()));

        context.close();
    }
}
AI 代码解读

控制台输出:

接受到了事件:class com.boot.event.eventdemo.MyApplicationEvent
接受到了事件:java.lang.Object@f713686
AI 代码解读

示例二(注解式,最常用)

1、自定义事件

public class MyApplicationEvent extends ApplicationEvent {
    public MyApplicationEvent(Object source) {
        super(source);
    }
}
AI 代码解读

2、@EventListener注解的方式监听

@Component
public class HandlerEvent {

    @EventListener(MyApplicationEvent.class)
    public void handlerEvent(MyApplicationEvent event) {
        System.out.println("接受到了事件====:"+event.getClass());
        System.out.println("接受到了事件====:"+event.getSource());
    }
}
AI 代码解读

3、使用容器中发布事件

@SpringBootApplication
public class EventDemoApplication {

    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(EventDemoApplication.class);
        ConfigurableApplicationContext context = app.run(args);
        // 发布事件
        context.publishEvent(new MyApplicationEvent(new Object()));

        context.close();
    }
}
AI 代码解读

控制台输出:

接受到了事件====:class com.boot.event.eventdemo.MyApplicationEvent
接受到了事件====:java.lang.Object@352c308
AI 代码解读

示例三(配置文件)

1、自定义事件

public class MyApplicationEvent extends ApplicationEvent {
    public MyApplicationEvent(Object source) {
        super(source);
    }
}
AI 代码解读

2、定义事件监听器

public class MyApplicationListener implements ApplicationListener<MyApplicationEvent> {

    @Override
    public void onApplicationEvent(MyApplicationEvent event) {
        System.out.println("接受到了事件:"+event.getClass());
        System.out.println("接受到了事件:"+event.getSource());
    }
}
AI 代码解读

3、使用容器中发布事件

@SpringBootApplication
public class EventDemoApplication {

    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(EventDemoApplication.class);
        ConfigurableApplicationContext context = app.run(args);
        // 发布事件
        context.publishEvent(new MyApplicationEvent(new Object()));

        context.close();
    }
}
AI 代码解读

4、application.properties中配置

context.listener.classes=com.boot.event.eventdemo.MyApplicationListener
AI 代码解读

5、控制台输出

接受到了事件:class com.boot.event.eventdemo.MyApplicationEvent
接受到了事件:java.lang.Object@3a0807b7
AI 代码解读
目录
打赏
0
0
0
0
513
分享
相关文章
Spring AI与DeepSeek实战三:打造企业知识库
本文基于Spring AI与RAG技术结合,通过构建实时知识库增强大语言模型能力,实现企业级智能搜索场景与个性化推荐,攻克LLM知识滞后与生成幻觉两大核心痛点。
218 7
微服务——SpringBoot使用归纳——Spring Boot中使用监听器——Spring Boot中自定义事件监听
本文介绍了在Spring Boot中实现自定义事件监听的完整流程。首先通过继承`ApplicationEvent`创建自定义事件,例如包含用户数据的`MyEvent`。接着,实现`ApplicationListener`接口构建监听器,用于捕获并处理事件。最后,在服务层通过`ApplicationContext`发布事件,触发监听器执行相应逻辑。文章结合微服务场景,展示了如何在微服务A处理完逻辑后通知微服务B,具有很强的实战意义。
55 0
Spring AI与DeepSeek实战四:系统API调用
在AI应用开发中,工具调用是增强大模型能力的核心技术,通过让模型与外部API或工具交互,可实现实时信息检索(如天气查询、新闻获取)、系统操作(如创建任务、发送邮件)等功能;本文结合Spring AI与大模型,演示如何通过Tool Calling实现系统API调用,同时处理多轮对话中的会话记忆。
206 57
深入解析HTTP请求方法:Spring Boot实战与最佳实践
这篇博客结合了HTTP规范、Spring Boot实现和实际工程经验,通过代码示例、对比表格和架构图等方式,系统性地讲解了不同HTTP方法的应用场景和最佳实践。
74 5
Spring AI与DeepSeek实战二:打造企业级智能体
本文介绍如何基于Spring AI与DeepSeek模型构建企业级多语言翻译智能体。通过明确的Prompt设计,该智能体能自主执行复杂任务,如精准翻译32种ISO标准语言,并严格遵循输入格式和行为限制。代码示例展示了如何通过API实现动态Prompt生成和翻译功能,确保服务的安全性和可控性。项目已开源,提供更多细节和完整代码。 [GitHub](https://github.com/zlt2000/zlt-spring-ai-app) | [Gitee](https://gitee.com/zlt2000/zlt-spring-ai-app)
205 11
Spring AI与DeepSeek实战一:快速打造智能对话应用
在 AI 技术蓬勃发展的今天,国产大模型DeepSeek凭借其低成本高性能的特点,成为企业智能化转型的热门选择。而Spring AI作为 Java 生态的 AI 集成框架,通过统一API、简化配置等特性,让开发者无需深入底层即可快速调用各类 AI 服务。本文将手把手教你通过spring-ai集成DeepSeek接口实现普通对话与流式对话功能,助力你的Java应用轻松接入 AI 能力!虽然通过Spring AI能够快速完成DeepSeek大模型与。
513 11
Spring Boot 两种部署到服务器的方式
本文介绍了Spring Boot项目的两种部署方式:jar包和war包。Jar包方式使用内置Tomcat,只需配置JDK 1.8及以上环境,通过`nohup java -jar`命令后台运行,并开放服务器端口即可访问。War包则需将项目打包后放入外部Tomcat的webapps目录,修改启动类继承`SpringBootServletInitializer`并调整pom.xml中的打包类型为war,最后启动Tomcat访问应用。两者各有优劣,jar包更简单便捷,而war包适合传统部署场景。需要注意的是,war包部署时,内置Tomcat的端口配置不会生效。
1194 17
Spring Boot 两种部署到服务器的方式
微服务——SpringBoot使用归纳——Spring Boot中的项目属性配置——指定项目配置文件
在实际项目中,开发环境和生产环境的配置往往不同。为简化配置切换,可通过创建 `application-dev.yml` 和 `application-pro.yml` 分别管理开发与生产环境配置,如设置不同端口(8001/8002)。在 `application.yml` 中使用 `spring.profiles.active` 指定加载的配置文件,实现环境快速切换。本节还介绍了通过配置类读取参数的方法,适用于微服务场景,提升代码可维护性。课程源码可从 [Gitee](https://gitee.com/eson15/springboot_study) 下载。
52 0
Jeesite5:Star24k,Spring Boot 3.3+Vue3实战开源项目,架构深度拆解!让企业级项目开发效率提升300%的秘密武器
Jeesite5 是一个基于 Spring Boot 3.3 和 Vue3 的企业级快速开发平台,集成了众多优秀开源项目,如 MyBatis Plus、Bootstrap、JQuery 等。它提供了模块化设计、权限管理、多数据库支持、代码生成器和国际化等功能,极大地提高了企业级项目的开发效率。Jeesite5 广泛应用于企业管理系统、电商平台、客户关系管理和知识管理等领域。通过其强大的功能和灵活性,Jeesite5 成为了企业级开发的首选框架之一。访问 [Gitee 页面](https://gitee.com/thinkgem/jeesite5) 获取更多信息。
Jeesite5:Star24k,Spring Boot 3.3+Vue3实战开源项目,架构深度拆解!让企业级项目开发效率提升300%的秘密武器
微服务——SpringBoot使用归纳——Spring Boot集成MyBatis——基于 xml 的整合
本教程介绍了基于XML的MyBatis整合方式。首先在`application.yml`中配置XML路径,如`classpath:mapper/*.xml`,然后创建`UserMapper.xml`文件定义SQL映射,包括`resultMap`和查询语句。通过设置`namespace`关联Mapper接口,实现如`getUserByName`的方法。Controller层调用Service完成测试,访问`/getUserByName/{name}`即可返回用户信息。为简化Mapper扫描,推荐在Spring Boot启动类用`@MapperScan`注解指定包路径避免逐个添加`@Mapper`
57 0