SpringBoot啟動(dòng)時(shí)加載指定方法的方式小結(jié)
1、Spring Boot啟動(dòng)時(shí)加載指定方法都有哪些方式的?
常用的方式有以下幾種:
?? 1. 實(shí)現(xiàn) CommandLineRunner 接口或 ApplicationRunner 接口:這兩個(gè)接口提供了一個(gè) run 方法,在應(yīng)用程序啟動(dòng)后立即執(zhí)行。您可以在這個(gè)方法中編寫您希望在應(yīng)用程序啟動(dòng)時(shí)執(zhí)行的代碼。
?? 2. 使用 @PostConstruct 注解:您可以將 @PostConstruct 注解添加到自定義類的方法上,該方法將在該類的實(shí)例被創(chuàng)建后立即執(zhí)行。這樣,您可以在該方法中編寫您希望在應(yīng)用程序啟動(dòng)時(shí)執(zhí)行的代碼。
?? 3. 使用 ApplicationListener 接口:您可以實(shí)現(xiàn) ApplicationListener 接口,并監(jiān)聽 ApplicationStartedEvent 事件。當(dāng)應(yīng)用程序啟動(dòng)時(shí),該事件將被觸發(fā),您可以在監(jiān)聽器中編寫您的自定義邏輯。
?? 4. 使用 @EventListener 注解:您可以將 @EventListener 注解添加到自定義類的方法上,并指定要監(jiān)聽的事件類型。當(dāng)指定的事件發(fā)生時(shí),該方法將被調(diào)用,您可以在其中編寫您的自定義邏輯。例如,您可以監(jiān)聽 ApplicationStartedEvent 事件。
2、CommandLineRunner 方式
CommandLineRunner 接口是Spring Boot提供的一個(gè)回調(diào)接口,可以在應(yīng)用程序啟動(dòng)后執(zhí)行特定的方法。您可以創(chuàng)建一個(gè)實(shí)現(xiàn) CommandLineRunner 接口的類,并重寫 run 方法,在其中編寫需要在啟動(dòng)時(shí)執(zhí)行的邏輯。
使用代碼如下:
package com.pany.camp.load;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
/**
*
* @description: CommandLineRunner
* @copyright: @Copyright (c) 2022
* @company: Aiocloud
* @author: pany
* @version: 1.0.0
* @createTime: 2023-07-06 21:54
*/
@Component
public class MyCommandLineRunner implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
// 在啟動(dòng)時(shí)執(zhí)行的邏輯
System.out.println("應(yīng)用程序啟動(dòng)時(shí)執(zhí)行的方法");
}
}3、ApplicationRunner 方式
ApplicationRunner 接口與 CommandLineRunner 接口類似,也是Spring Boot提供的一個(gè)回調(diào)接口,用于在應(yīng)用程序啟動(dòng)后執(zhí)行特定的方法。與 CommandLineRunner 不同的是, ApplicationRunner 的 run 方法接收一個(gè) ApplicationArguments 對(duì)象,可以用于獲取應(yīng)用程序啟動(dòng)參數(shù)。
使用代碼如下:
package com.pany.camp.load;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
/**
*
* @description: ApplicationRunner
* @copyright: @Copyright (c) 2022
* @company: Aiocloud
* @author: pany
* @version: 1.0.0
* @createTime: 2023-07-06 22:04
*/
@Component
public class MyApplicationRunner implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) throws Exception {
// 在啟動(dòng)時(shí)執(zhí)行的邏輯
System.out.println("應(yīng)用程序啟動(dòng)時(shí)執(zhí)行的方法");
}
}4、@PostConstruct 方式
@PostConstruct 注解是Java EE提供的一個(gè)標(biāo)準(zhǔn)注解,用于指定在構(gòu)造函數(shù)執(zhí)行完畢后立即執(zhí)行的方法。在Spring Boot中,您可以在任何一個(gè)Bean中使用 @PostConstruct 注解來(lái)標(biāo)記需要在啟動(dòng)時(shí)執(zhí)行的方法。
使用代碼如下:
package com.pany.camp.load;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
/**
*
* @description: PostConstruct
* @copyright: @Copyright (c) 2022
* @company: Aiocloud
* @author: pany
* @version: 1.0.0
* @createTime: 2023-07-06 22:07
*/
@Component
public class MyPostConstruct {
@PostConstruct
public void init() {
// 在啟動(dòng)時(shí)執(zhí)行的邏輯
System.out.println("應(yīng)用程序啟動(dòng)時(shí)執(zhí)行的方法");
}
}5、ApplicationListener 方式
ApplicationListener 是 Spring Framework 提供的一個(gè)接口,用于監(jiān)聽?wèi)?yīng)用程序中的事件并執(zhí)行相應(yīng)的邏輯。您可以實(shí)現(xiàn) ApplicationListener 接口,并重寫 onApplicationEvent 方法來(lái)處理特定的事件。
使用代碼如下:
package com.pany.camp.load;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
/**
*
* @description: ApplicationListener
* @copyright: @Copyright (c) 2022
* @company: Aiocloud
* @author: pany
* @version: 1.0.0
* @createTime: 2023-07-06 22:09
*/
@Component
public class MyEventListener implements ApplicationListener<ApplicationEvent> {
@Override
public void onApplicationEvent(ApplicationEvent event) {
// 在這里處理特定的事件邏輯
System.out.println("收到應(yīng)用程序事件:" + event.toString());
}
}在上述示例中, MyEventListener 類實(shí)現(xiàn)了 ApplicationListener 接口,并指定了泛型參數(shù)為 ApplicationEvent ,表示監(jiān)聽所有類型的應(yīng)用程序事件。您可以根據(jù)實(shí)際需求,將泛型參數(shù)指定為特定的事件類型。
當(dāng)應(yīng)用程序觸發(fā)事件時(shí), onApplicationEvent 方法會(huì)被調(diào)用,并傳入相應(yīng)的事件對(duì)象。您可以在該方法中編寫處理事件的邏輯。
要注意的是,為了讓 Spring Boot 自動(dòng)注冊(cè) MyEventListener ,需要將其標(biāo)記為 @Component 或使用其他方式將其納入 Spring 容器的管理范圍。
6、@EventListener 方式
@EventListener 注解可以用于監(jiān)聽 Spring 應(yīng)用程序中的各種事件,包括啟動(dòng)事件。當(dāng)應(yīng)用程序啟動(dòng)時(shí),帶有 @EventListener 注解的方法會(huì)被自動(dòng)觸發(fā)。
使用代碼如下:
package com.pany.camp.load;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
/**
*
* @description: @EventListener
* @copyright: @Copyright (c) 2022
* @company: Aiocloud
* @author: pany
* @version: 1.0.0
* @createTime: 2023-07-06 22:11
*/
@Component
public class MyEventListenerExample {
@EventListener
public void handleContextRefresh(ContextRefreshedEvent event) {
// 在這里添加應(yīng)用程序啟動(dòng)時(shí)的邏輯
System.out.println(" @EventListener 應(yīng)用程序已啟動(dòng)!" + event.toString());
}
}上述示例中, handleContextRefresh 方法被標(biāo)記為 @EventListener ,并指定了 ContextRefreshedEvent 類型的事件。當(dāng)應(yīng)用程序啟動(dòng)并完成刷新時(shí),該方法會(huì)被自動(dòng)觸發(fā)。
您可以在 handleContextRefresh 方法中添加應(yīng)用程序啟動(dòng)時(shí)需要執(zhí)行的邏輯。例如,您可以初始化一些數(shù)據(jù)、啟動(dòng)定時(shí)任務(wù)等。
請(qǐng)注意,為了讓 Spring Boot 自動(dòng)注冊(cè) MyApplication 類中的事件監(jiān)聽器,需要將其標(biāo)記為 @SpringBootApplication 或使用其他方式將其納入 Spring 容器的管理范圍。
以上就是SpringBoot啟動(dòng)時(shí)加載指定方法的方式小結(jié)的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot加載指定方法的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- SpringBoot啟動(dòng)時(shí)自動(dòng)執(zhí)行指定方法的幾種實(shí)現(xiàn)方式
- IntelliJ IDEA下SpringBoot如何指定某一個(gè)配置文件啟動(dòng)項(xiàng)目
- springBoot?啟動(dòng)指定配置文件環(huán)境多種方案(最新推薦)
- IDEA下SpringBoot指定環(huán)境、配置文件啟動(dòng)操作過程
- SpringBoot啟動(dòng)時(shí)如何通過啟動(dòng)參數(shù)指定logback的位置
- springboot指定profiles啟動(dòng)失敗問題及解決
- springboot 項(xiàng)目容器啟動(dòng)后如何自動(dòng)執(zhí)行指定方法
- springboot項(xiàng)目啟動(dòng)指定對(duì)應(yīng)環(huán)境的方法
- SpringBoot項(xiàng)目啟動(dòng)時(shí)執(zhí)行指定的方法
相關(guān)文章
Idea 解決 Could not autowire. No beans of ''xxxx'' type found
這篇文章主要介紹了Idea 解決 Could not autowire. No beans of 'xxxx' type found 的錯(cuò)誤提示,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2019-01-01
解決SpringCloud Gateway采用OpenFeign遠(yuǎn)程調(diào)用失敗的問題
在使用SpringCloud網(wǎng)關(guān)進(jìn)行統(tǒng)一鑒權(quán)和認(rèn)證過程中,通過OpenFeign遠(yuǎn)程調(diào)用鑒權(quán)服務(wù)器接口時(shí)可能會(huì)遇到遠(yuǎn)程調(diào)用失敗的問題,這通常是因?yàn)镠ttpMessageConverters沒有被正確注入到Spring容器中2024-09-09
java中優(yōu)化大量if...else...方法總結(jié)
在我們平時(shí)的開發(fā)過程中,經(jīng)??赡軙?huì)出現(xiàn)大量If else的場(chǎng)景,代碼顯的很臃腫,非常不優(yōu)雅,下面這篇文章主要給大家介紹了關(guān)于java中優(yōu)化大量if...else...方法的相關(guān)資料,需要的朋友可以參考下2023-03-03
Java使用DateTimeFormatter格式化輸入的日期時(shí)間
這篇文章主要介紹了Java使用DateTimeFormatter格式化輸入的日期時(shí)間,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01
Java運(yùn)算符解密之位運(yùn)算、移位運(yùn)算舉例詳解
這篇文章主要介紹了Java運(yùn)算符解密之位運(yùn)算、移位運(yùn)算的相關(guān)資料,Java中的位運(yùn)算符包括按位與&、按位或|、按位取反~和按位異或^,用于對(duì)數(shù)據(jù)的二進(jìn)制位進(jìn)行操作,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2025-04-04
spring mvc中的@PathVariable獲得請(qǐng)求url中的動(dòng)態(tài)參數(shù)
本文主要介紹了spring mvc中的@PathVariable獲得請(qǐng)求url中的動(dòng)態(tài)參數(shù)的代碼。具有很好的參考價(jià)值,下面跟著小編一起來(lái)看下吧2017-02-02

