springBoot 自定义监听器的几种方式

 

 

第一种:在初始化的时候添加监听器 app.addListeners(new MyApplicationListener());

第二种:

第三种:在application.propertiies 添加

context.listener.classes=com.boot.enable.addlistener.MyApplicationListener

第四种:

@Component
public class HandlerEvent {

    @EventListener
    public void evendListener(MyApplicationEvent event){
        System.out.println("监听了-----"+event.getSource());
        System.out.println("监听了-----"+event.getClass());
    }

}
### 实现 Spring Boot 3 中的监听器功能 在 Spring Boot 3 中,可以通过多种方式实现监听器功能。以下是几种常见的方法及其具体实现: #### 方法一:通过 `@EventListener` 注解实现事件监听 Spring 提供了内置的支持来处理自定义事件和标准 Java 应用程序生命周期事件。可以使用 `@EventListener` 注解在一个普通的组件类中注册一个方法作为事件处理器。 ```java import org.springframework.context.event.EventListener; import org.springframework.stereotype.Component; @Component public class MyApplicationListener { @EventListener(ApplicationReadyEvent.class) public void handleApplicationReadyEvent(ApplicationReadyEvent event) { System.out.println("The application is now ready!"); } } ``` 上述代码展示了如何监听 `ApplicationReadyEvent`,这是 Spring Boot 启动完成后触发的一个事件[^1]。 --- #### 方法二:继承 `ApplicationListener<T>` 接口 另一种更传统的实现方式是让某个类实现 `ApplicationListener<T>` 接口,并覆盖其中的方法以响应特定类型的事件。 ```java import org.springframework.boot.context.event.ApplicationStartedEvent; import org.springframework.context.ApplicationListener; import org.springframework.stereotype.Component; @Component public class ApplicationStartupListener implements ApplicationListener<ApplicationStartedEvent> { @Override public void onApplicationEvent(ApplicationStartedEvent event) { System.out.println("The application has started."); } } ``` 这种方式适用于需要更加精细控制的情况,比如当需要访问事件对象中的某些属性时[^2]。 --- #### 方法三:发布自定义事件监听 除了监听框架自带的事件外,还可以创建自己的事件并通过 `ApplicationEventPublisher` 发布它们。 ##### 定义自定义事件 ```java import org.springframework.context.ApplicationEvent; public class CustomEvent extends ApplicationEvent { private String message; public CustomEvent(Object source, String message) { super(source); this.message = message; } public String getMessage() { return message; } } ``` ##### 创建监听器 ```java import org.springframework.context.event.EventListener; import org.springframework.stereotype.Component; @Component public class CustomEventListener { @EventListener(CustomEvent.class) public void handleCustomEvent(CustomEvent event) { System.out.println("Received custom event with message: " + event.getMessage()); } } ``` ##### 发布自定义事件 ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.context.ApplicationEventPublisher; import org.springframework.stereotype.Component; @Component public class EventPublisherExample implements CommandLineRunner { @Autowired private ApplicationEventPublisher publisher; @Override public void run(String... args) throws Exception { CustomEvent customEvent = new CustomEvent(this, "This is a test message"); publisher.publishEvent(customEvent); } } ``` 这种方法允许开发者完全掌控事件的内容以及何时何地触发这些事件[^3]。 --- #### 配置独立数据库实例用于测试环境 如果计划在测试环境中使用多个应用上下文而不想共享同一数据库连接,则可以在配置文件中启用如下选项: ```properties spring.datasource.generate-unique-name=true ``` 这会确保每次运行新的上下文时都会生成一个新的嵌入式数据库实例[^4]。 --- ### 总结 以上介绍了三种主要的方式来实现在 Spring Boot 3 中的监听器功能。无论是基于注解还是接口的方式都可以很好地满足需求,同时支持扩展来自定义化行为。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值