SpringBoot监听器的实现示例
在Spring Boot中,你可以使用监听器来响应特定的事件。这些事件可以是Spring Boot应用生命周期中的某个阶段(如启动、关闭等),也可以是你自定义的业务事件。
1. 创建一个监听器
创建一个监听器有两种方法:实现ApplicationListener接口或使用@EventListener注解。
实现ApplicationListener接口
创建一个新的类并实现ApplicationListener接口,传入你想要监听的事件类型作为泛型参数。
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
@Component
public class MyApplicationListener implements ApplicationListener<ApplicationReadyEvent> {
@Override
public void onApplicationEvent(ApplicationReadyEvent event) {
System.out.println("Application is ready!");
}
}
在这个例子中,我们创建了一个名为MyApplicationListener的监听器,它会在Spring Boot应用准备好之后执行一些操作(打印一条消息)。
使用@EventListener注解
你也可以通过在方法上添加@EventListener注解来创建一个监听器:
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
@Component
public class MyEventListener {
@EventListener
public void handleApplicationReadyEvent(ApplicationReadyEvent event) {
System.out.println("Application is ready!");
}
}
2. 自定义事件
在 Spring Boot 中,自定义监听事件的步骤如下:
- 创建一个自定义事件类。这个类需要继承
ApplicationEvent类。
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;
}
}
在这个例子中,我们创建了一个名为 CustomEvent 的自定义事件类,它包含了一个字符串类型的 message 属性,用于存储事件的相关信息。
- 创建一个监听器类,实现
ApplicationListener接口,并指定要监听的事件类型(这里是CustomEvent):
import org.springframework.context.ApplicationListener;
public class CustomEventListener implements ApplicationListener<CustomEvent> {
@Override
public void onApplicationEvent(CustomEvent event) {
System.out.println("Received custom event: " + event.getMessage());
}
}
注册监听器到 Spring 容器。你可以使用自动扫描、手动注册 Bean 或使用
@EventListener注解来完成这一任务。发布自定义事件:现在你已经创建了自定义事件和监听器,可以使用
ApplicationContext来发布你的自定义事件:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class EventPublisher {
@Autowired
private final ApplicationContext context;
public EventPublisher(ApplicationContext context) {
this.context = context;
}
public void publishCustomEvent(String message) {
context.publishEvent(new CustomEvent(this, message));
}
}
在这个示例中,我们创建了一个 EventPublisher 类,它有一个注入的 ApplicationContext。当调用 publishCustomEvent() 方法时,会发布一个新的 CustomEvent,并将消息传递给监听器。
- 最后,在需要的地方调用
EventPublisher类的publishCustomEvent()方法来触发事件:
@Autowired
private EventPublisher eventPublisher;
// ...
eventPublisher.publishCustomEvent("Hello from a custom event!");
这样,每当 publishCustomEvent() 被调用时,你的自定义监听器就会接收到事件并执行相应的操作。
3.springboot使用定时任务
这种方式很简单,主要就是先@EnableScheduling开启定时任务功能,然后在相应的方法上添加@Scheduled()中间写上相应的cron表达式即可。示例如下:
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
@Configuration //1.主要用于标记配置类,兼备Component的效果。
@EnableScheduling // 2.开启定时任务
public class ScheduleTask {
@Scheduled(cron = "0/5 * * * * ?") //定时任务注解+cron表达式
public void testScheduleTask() {
System.out.println("执行定时任务" + LocalDateTime.now());
}
}到此这篇关于SpringBoot监听器的实现示例的文章就介绍到这了,更多相关SpringBoot监听器内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
Java中的日期时间类实例详解(Date、Calendar、DateFormat)
在JDK1.0中,Date类是唯一的一个代表时间的类,但是由于Date类不便于实现国际化,所以从JDK1.1版本开始,推荐使用Calendar类进行时间和日期处理,这篇文章主要介绍了Java中的日期时间类详解(Date、Calendar、DateFormat),需要的朋友可以参考下2023-11-11
SpringMVC的处理器拦截器HandlerInterceptor详解
这篇文章主要介绍了SpringMVC的处理器拦截器HandlerInterceptor详解,SpringWebMVC的处理器拦截器,类似于Servlet开发中的过滤器Filter,用于处理器进行预处理和后处理,需要的朋友可以参考下2024-01-01
SpringBoot Maven项目依赖冲突问题排查与解决全攻略
本文将深入剖析SpringBoot项目中关于Maven依赖冲突问题的排查与解决,文内会提供三种实用排查方案与实战代码,希望可以帮助大家彻底搞定依赖冲突2026-03-03
idea运行java的配置详细教程(包含maven,mysql下载配置)
程序员们在开发的时候,一定会用到Intellij IDEA这个集成开发环境,这篇文章主要给大家介绍了关于idea运行java的配置(包含maven,mysql下载配置)的相关资料,需要的朋友可以参考下2024-05-05


最新评论