目录
- 1. 创建一个监听器
- 实现ApplicationListener接口
- 使用@EventListener注解
- 2. 自定义事件
- 3.springboot使用定时任务
在Spring Boot中,你可以使用监听器来响应特定的事件。这些事件可以是Spring Boot应用生命周期中的某个阶段(如启动、关javascript闭等),也可编程客栈以是你自定义的业务事件。
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 中,自定义监听事件的步骤如下:
- 创建一个自定义事件类。这个类需要继承 http://www.devze.com
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 onApplic编程客栈ationEvent(CustomEvent event) {
System.out.println("Received custom event: " + event.getMessage());
}
}
注册监听器到 Spring 容器。你可以使用自动扫描、手动注册 Bean 或使用
@EventListener注解来完成这一任务。发布自定义事件:现在你已经创建了自定义事件和监听器,可以使用
ApplicationContext来发布你的自定义事件:
import org.springframework.beans.fact编程客栈ory.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监听器内容请搜索编程客栈(www.devze.com)以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程客栈(www.devze.com)!
加载中,请稍侯......
精彩评论