SpringBoot監(jiān)聽器的實現(xiàn)示例
在Spring Boot中,你可以使用監(jiān)聽器來響應特定的事件。這些事件可以是Spring Boot應用生命周期中的某個階段(如啟動、關閉等),也可以是你自定義的業(yè)務事件。
1. 創(chuàng)建一個監(jiān)聽器
創(chuàng)建一個監(jiān)聽器有兩種方法:實現(xiàn)ApplicationListener接口或使用@EventListener注解。
實現(xiàn)ApplicationListener接口
創(chuàng)建一個新的類并實現(xiàn)ApplicationListener接口,傳入你想要監(jiān)聽的事件類型作為泛型參數(shù)。
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!");
}
}
在這個例子中,我們創(chuàng)建了一個名為MyApplicationListener的監(jiān)聽器,它會在Spring Boot應用準備好之后執(zhí)行一些操作(打印一條消息)。
使用@EventListener注解
你也可以通過在方法上添加@EventListener注解來創(chuàng)建一個監(jiān)聽器:
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 中,自定義監(jiān)聽事件的步驟如下:
- 創(chuàng)建一個自定義事件類。這個類需要繼承
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;
}
}
在這個例子中,我們創(chuàng)建了一個名為 CustomEvent 的自定義事件類,它包含了一個字符串類型的 message 屬性,用于存儲事件的相關信息。
- 創(chuàng)建一個監(jiān)聽器類,實現(xiàn)
ApplicationListener接口,并指定要監(jiān)聽的事件類型(這里是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());
}
}
注冊監(jiān)聽器到 Spring 容器。你可以使用自動掃描、手動注冊 Bean 或使用
@EventListener注解來完成這一任務。發(fā)布自定義事件:現(xiàn)在你已經(jīng)創(chuàng)建了自定義事件和監(jiān)聽器,可以使用
ApplicationContext來發(fā)布你的自定義事件:
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));
}
}
在這個示例中,我們創(chuàng)建了一個 EventPublisher 類,它有一個注入的 ApplicationContext。當調(diào)用 publishCustomEvent() 方法時,會發(fā)布一個新的 CustomEvent,并將消息傳遞給監(jiān)聽器。
- 最后,在需要的地方調(diào)用
EventPublisher類的publishCustomEvent()方法來觸發(fā)事件:
@Autowired
private EventPublisher eventPublisher;
// ...
eventPublisher.publishCustomEvent("Hello from a custom event!");
這樣,每當 publishCustomEvent() 被調(diào)用時,你的自定義監(jiān)聽器就會接收到事件并執(zhí)行相應的操作。
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.主要用于標記配置類,兼?zhèn)銫omponent的效果。
@EnableScheduling // 2.開啟定時任務
public class ScheduleTask {
@Scheduled(cron = "0/5 * * * * ?") //定時任務注解+cron表達式
public void testScheduleTask() {
System.out.println("執(zhí)行定時任務" + LocalDateTime.now());
}
}到此這篇關于SpringBoot監(jiān)聽器的實現(xiàn)示例的文章就介紹到這了,更多相關SpringBoot監(jiān)聽器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
SpringBoot 在IDEA中實現(xiàn)熱部署步驟詳解(實用版)
這篇文章主要介紹了SpringBoot 在IDEA中實現(xiàn)熱部署步驟詳解(實用版),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-12-12
idea熱部署插件jrebel正式版及破解版安裝詳細圖文教程
這篇文章主要介紹了idea熱部署插件jrebel正式版及破解版安裝詳細教程,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-12-12
springboot druid數(shù)據(jù)庫連接池連接失敗后一直重連的解決方法
本文主要介紹了springboot druid數(shù)據(jù)庫連接池連接失敗后一直重連的解決方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-04-04
java8 stream 操作map根據(jù)key或者value排序的實現(xiàn)
這篇文章主要介紹了java8 stream 操作map根據(jù)key或者value排序的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-09-09

