SpringBoot啟動時加載指定方法的方式小結(jié)
1、Spring Boot啟動時加載指定方法都有哪些方式的?
常用的方式有以下幾種:
?? 1. 實(shí)現(xiàn) CommandLineRunner 接口或 ApplicationRunner 接口:這兩個接口提供了一個 run 方法,在應(yīng)用程序啟動后立即執(zhí)行。您可以在這個方法中編寫您希望在應(yīng)用程序啟動時執(zhí)行的代碼。
?? 2. 使用 @PostConstruct 注解:您可以將 @PostConstruct 注解添加到自定義類的方法上,該方法將在該類的實(shí)例被創(chuàng)建后立即執(zhí)行。這樣,您可以在該方法中編寫您希望在應(yīng)用程序啟動時執(zhí)行的代碼。
?? 3. 使用 ApplicationListener 接口:您可以實(shí)現(xiàn) ApplicationListener 接口,并監(jiān)聽 ApplicationStartedEvent 事件。當(dāng)應(yīng)用程序啟動時,該事件將被觸發(fā),您可以在監(jiān)聽器中編寫您的自定義邏輯。
?? 4. 使用 @EventListener 注解:您可以將 @EventListener 注解添加到自定義類的方法上,并指定要監(jiān)聽的事件類型。當(dāng)指定的事件發(fā)生時,該方法將被調(diào)用,您可以在其中編寫您的自定義邏輯。例如,您可以監(jiān)聽 ApplicationStartedEvent 事件。
2、CommandLineRunner 方式
CommandLineRunner 接口是Spring Boot提供的一個回調(diào)接口,可以在應(yīng)用程序啟動后執(zhí)行特定的方法。您可以創(chuàng)建一個實(shí)現(xiàn) CommandLineRunner 接口的類,并重寫 run 方法,在其中編寫需要在啟動時執(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 {
// 在啟動時執(zhí)行的邏輯
System.out.println("應(yīng)用程序啟動時執(zhí)行的方法");
}
}3、ApplicationRunner 方式
ApplicationRunner 接口與 CommandLineRunner 接口類似,也是Spring Boot提供的一個回調(diào)接口,用于在應(yīng)用程序啟動后執(zhí)行特定的方法。與 CommandLineRunner 不同的是, ApplicationRunner 的 run 方法接收一個 ApplicationArguments 對象,可以用于獲取應(yī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 {
// 在啟動時執(zhí)行的邏輯
System.out.println("應(yīng)用程序啟動時執(zhí)行的方法");
}
}4、@PostConstruct 方式
@PostConstruct 注解是Java EE提供的一個標(biāo)準(zhǔn)注解,用于指定在構(gòu)造函數(shù)執(zhí)行完畢后立即執(zhí)行的方法。在Spring Boot中,您可以在任何一個Bean中使用 @PostConstruct 注解來標(biāo)記需要在啟動時執(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() {
// 在啟動時執(zhí)行的邏輯
System.out.println("應(yīng)用程序啟動時執(zhí)行的方法");
}
}5、ApplicationListener 方式
ApplicationListener 是 Spring Framework 提供的一個接口,用于監(jiān)聽?wèi)?yīng)用程序中的事件并執(zhí)行相應(yīng)的邏輯。您可以實(shí)現(xiàn) ApplicationListener 接口,并重寫 onApplicationEvent 方法來處理特定的事件。
使用代碼如下:
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ā)事件時, onApplicationEvent 方法會被調(diào)用,并傳入相應(yīng)的事件對象。您可以在該方法中編寫處理事件的邏輯。
要注意的是,為了讓 Spring Boot 自動注冊 MyEventListener ,需要將其標(biāo)記為 @Component 或使用其他方式將其納入 Spring 容器的管理范圍。
6、@EventListener 方式
@EventListener 注解可以用于監(jiān)聽 Spring 應(yīng)用程序中的各種事件,包括啟動事件。當(dāng)應(yīng)用程序啟動時,帶有 @EventListener 注解的方法會被自動觸發(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)用程序啟動時的邏輯
System.out.println(" @EventListener 應(yīng)用程序已啟動!" + event.toString());
}
}上述示例中, handleContextRefresh 方法被標(biāo)記為 @EventListener ,并指定了 ContextRefreshedEvent 類型的事件。當(dāng)應(yīng)用程序啟動并完成刷新時,該方法會被自動觸發(fā)。
您可以在 handleContextRefresh 方法中添加應(yīng)用程序啟動時需要執(zhí)行的邏輯。例如,您可以初始化一些數(shù)據(jù)、啟動定時任務(wù)等。
請注意,為了讓 Spring Boot 自動注冊 MyApplication 類中的事件監(jiān)聽器,需要將其標(biāo)記為 @SpringBootApplication 或使用其他方式將其納入 Spring 容器的管理范圍。
以上就是SpringBoot啟動時加載指定方法的方式小結(jié)的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot加載指定方法的資料請關(guān)注腳本之家其它相關(guān)文章!
- SpringBoot啟動時自動執(zhí)行指定方法的幾種實(shí)現(xiàn)方式
- IntelliJ IDEA下SpringBoot如何指定某一個配置文件啟動項(xiàng)目
- springBoot?啟動指定配置文件環(huán)境多種方案(最新推薦)
- IDEA下SpringBoot指定環(huán)境、配置文件啟動操作過程
- SpringBoot啟動時如何通過啟動參數(shù)指定logback的位置
- springboot指定profiles啟動失敗問題及解決
- springboot 項(xiàng)目容器啟動后如何自動執(zhí)行指定方法
- springboot項(xiàng)目啟動指定對應(yīng)環(huán)境的方法
- SpringBoot項(xiàng)目啟動時執(zhí)行指定的方法
相關(guān)文章
實(shí)例解析Java設(shè)計模式編程中的適配器模式使用
適配器模式的主要作用是在新接口和老接口之間進(jìn)行適配,通過將一個類的接口轉(zhuǎn)換成客戶期望的另一個接口,讓原本不兼容的接口可以合作無間,本文以實(shí)例解析Java設(shè)計模式編程中的適配器模式使用,需要的朋友可以參考下2016-05-05
基于Jackson實(shí)現(xiàn)API接口數(shù)據(jù)脫敏的示例詳解
用戶的一些敏感數(shù)據(jù),例如手機(jī)號、郵箱、身份證等信息,在數(shù)據(jù)庫以明文存儲,但在接口返回數(shù)據(jù)給瀏覽器(或三方客戶端)時,希望對這些敏感數(shù)據(jù)進(jìn)行脫敏,所以本文就給大家介紹以惡如何利用Jackson實(shí)現(xiàn)API接口數(shù)據(jù)脫敏,需要的朋友可以參考下2023-08-08
MyBatis整合Redis實(shí)現(xiàn)二級緩存的示例代碼
這篇文章主要介紹了MyBatis整合Redis實(shí)現(xiàn)二級緩存的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
Java實(shí)現(xiàn)提取不重復(fù)的整數(shù)實(shí)例
這篇文章主要介紹了Java實(shí)現(xiàn)提取不重復(fù)的整數(shù)實(shí)例,具有一定借鑒價值,需要的朋友可以參考下2017-12-12
SpringBoot使用RestTemplate實(shí)現(xiàn)HTTP請求詳解
這篇文章主要為大家詳細(xì)介紹了SpringBoot如何使用RestTemplate實(shí)現(xiàn)進(jìn)行HTTP請求,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-03-03
詳解SpringCloud mysql實(shí)現(xiàn)配置中心
這篇文章主要介紹了詳解SpringCloud mysql實(shí)現(xiàn)配置中心,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-09-09

