SpringBoot監(jiān)聽器的實(shí)現(xiàn)示例
在Spring Boot中,你可以使用監(jiān)聽器來(lái)響應(yīng)特定的事件。這些事件可以是Spring Boot應(yīng)用生命周期中的某個(gè)階段(如啟動(dòng)、關(guān)閉等),也可以是你自定義的業(yè)務(wù)事件。
1. 創(chuàng)建一個(gè)監(jiān)聽器
創(chuàng)建一個(gè)監(jiān)聽器有兩種方法:實(shí)現(xiàn)ApplicationListener接口或使用@EventListener注解。
實(shí)現(xiàn)ApplicationListener接口
創(chuàng)建一個(gè)新的類并實(shí)現(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!");
}
}
在這個(gè)例子中,我們創(chuàng)建了一個(gè)名為MyApplicationListener的監(jiān)聽器,它會(huì)在Spring Boot應(yīng)用準(zhǔn)備好之后執(zhí)行一些操作(打印一條消息)。
使用@EventListener注解
你也可以通過(guò)在方法上添加@EventListener注解來(lái)創(chuàng)建一個(gè)監(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)建一個(gè)自定義事件類。這個(gè)類需要繼承
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;
}
}
在這個(gè)例子中,我們創(chuàng)建了一個(gè)名為 CustomEvent 的自定義事件類,它包含了一個(gè)字符串類型的 message 屬性,用于存儲(chǔ)事件的相關(guān)信息。
- 創(chuàng)建一個(gè)監(jiān)聽器類,實(shí)現(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());
}
}
注冊(cè)監(jiān)聽器到 Spring 容器。你可以使用自動(dòng)掃描、手動(dòng)注冊(cè) Bean 或使用
@EventListener注解來(lái)完成這一任務(wù)。發(fā)布自定義事件:現(xiàn)在你已經(jīng)創(chuàng)建了自定義事件和監(jiān)聽器,可以使用
ApplicationContext來(lái)發(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));
}
}
在這個(gè)示例中,我們創(chuàng)建了一個(gè) EventPublisher 類,它有一個(gè)注入的 ApplicationContext。當(dāng)調(diào)用 publishCustomEvent() 方法時(shí),會(huì)發(fā)布一個(gè)新的 CustomEvent,并將消息傳遞給監(jiān)聽器。
- 最后,在需要的地方調(diào)用
EventPublisher類的publishCustomEvent()方法來(lái)觸發(fā)事件:
@Autowired
private EventPublisher eventPublisher;
// ...
eventPublisher.publishCustomEvent("Hello from a custom event!");
這樣,每當(dāng) publishCustomEvent() 被調(diào)用時(shí),你的自定義監(jiān)聽器就會(huì)接收到事件并執(zhí)行相應(yīng)的操作。
3.springboot使用定時(shí)任務(wù)
這種方式很簡(jiǎn)單,主要就是先@EnableScheduling開啟定時(shí)任務(wù)功能,然后在相應(yīng)的方法上添加@Scheduled()中間寫上相應(yīng)的cron表達(dá)式即可。示例如下:
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
@Configuration //1.主要用于標(biāo)記配置類,兼?zhèn)銫omponent的效果。
@EnableScheduling // 2.開啟定時(shí)任務(wù)
public class ScheduleTask {
@Scheduled(cron = "0/5 * * * * ?") //定時(shí)任務(wù)注解+cron表達(dá)式
public void testScheduleTask() {
System.out.println("執(zhí)行定時(shí)任務(wù)" + LocalDateTime.now());
}
}到此這篇關(guān)于SpringBoot監(jiān)聽器的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)SpringBoot監(jiān)聽器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot 在IDEA中實(shí)現(xiàn)熱部署步驟詳解(實(shí)用版)
這篇文章主要介紹了SpringBoot 在IDEA中實(shí)現(xiàn)熱部署步驟詳解(實(shí)用版),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-12-12
SWT(JFace) FTP客戶端實(shí)現(xiàn)
SWT(JFace)小制作:FTP客戶端實(shí)現(xiàn)2009-06-06
使用SpringBoot進(jìn)行身份驗(yàn)證和授權(quán)的示例詳解
在廣闊的 Web 開發(fā)世界中,身份驗(yàn)證是每個(gè)數(shù)字領(lǐng)域的守護(hù)者,在本教程中,我們將了解如何以本機(jī)方式保護(hù)、驗(yàn)證和授權(quán) Spring-Boot 應(yīng)用程序的用戶,并遵循框架的良好實(shí)踐,希望對(duì)大家有所幫助2023-11-11
idea熱部署插件jrebel正式版及破解版安裝詳細(xì)圖文教程
這篇文章主要介紹了idea熱部署插件jrebel正式版及破解版安裝詳細(xì)教程,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12
springboot druid數(shù)據(jù)庫(kù)連接池連接失敗后一直重連的解決方法
本文主要介紹了springboot druid數(shù)據(jù)庫(kù)連接池連接失敗后一直重連的解決方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04
java8 stream 操作map根據(jù)key或者value排序的實(shí)現(xiàn)
這篇文章主要介紹了java8 stream 操作map根據(jù)key或者value排序的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09

