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)聽(tīng) ApplicationStartedEvent 事件。當(dāng)應(yīng)用程序啟動(dòng)時(shí),該事件將被觸發(fā),您可以在監(jiān)聽(tīng)器中編寫您的自定義邏輯。
?? 4. 使用 @EventListener 注解:您可以將 @EventListener 注解添加到自定義類的方法上,并指定要監(jiān)聽(tīng)的事件類型。當(dāng)指定的事件發(fā)生時(shí),該方法將被調(diào)用,您可以在其中編寫您的自定義邏輯。例如,您可以監(jiān)聽(tīng) 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)聽(tīng)?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)聽(tīng)所有類型的應(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)聽(tīng) 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)聽(tīng)器,需要將其標(biāo)記為 @SpringBootApplication 或使用其他方式將其納入 Spring 容器的管理范圍。
以上就是SpringBoot啟動(dòng)時(shí)加載指定方法的方式小結(jié)的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot加載指定方法的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
spring boot輸入數(shù)據(jù)校驗(yàn)(validation)的實(shí)現(xiàn)過(guò)程
web項(xiàng)目中,用戶的輸入總是被假定不安全不正確的,在被處理前需要做校驗(yàn)。本文介紹在spring boot項(xiàng)目中實(shí)現(xiàn)數(shù)據(jù)校驗(yàn)的過(guò)程,通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧2021-09-09Java Swing 非常漂亮外觀Nimbus的使用方法實(shí)例
Java Swing 非常漂亮外觀Nimbus的使用方法實(shí)例,需要的朋友可以參考一下2013-02-02springboot時(shí)間格式化的五種方法總結(jié)(解決后端傳給前端的時(shí)間顯示不一致)
這篇文章主要給大家介紹了關(guān)于springboot時(shí)間格式化的五種方法,文中介紹的方法解決了后端傳給前端的時(shí)間顯示不一致,文中通過(guò)圖文以及代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-01-01SpringMVC實(shí)現(xiàn)用戶登錄全過(guò)程
這篇文章主要介紹了SpringMVC實(shí)現(xiàn)用戶登錄全過(guò)程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-09-09java使用Socket實(shí)現(xiàn)SMTP協(xié)議發(fā)送郵件
這篇文章主要為大家詳細(xì)介紹了java使用Socket實(shí)現(xiàn)SMTP協(xié)議發(fā)送郵件的相關(guān)資料,感興趣的小伙伴們可以參考一下2016-05-05spring security在分布式項(xiàng)目下的配置方法(案例詳解)
這篇文章主要介紹了spring security在分布式項(xiàng)目下的配置方法,本文通過(guò)一個(gè)項(xiàng)目案例給大家詳細(xì)介紹,通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10Elasticsearch進(jìn)行深度分頁(yè)的詳細(xì)指南(避免踩坑+報(bào)錯(cuò))
這篇文章主要為大家詳細(xì)介紹了使用Elasticsearch進(jìn)行深度分頁(yè)時(shí)會(huì)員踩的坑以及報(bào)錯(cuò)的相關(guān)解決方法,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2025-04-04解決Jackson反序列化map,set等復(fù)雜類型問(wèn)題
這篇文章主要介紹了解決Jackson反序列化map,set等復(fù)雜類型問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-09-09spring關(guān)于組件的注入及獲取流程場(chǎng)景分析
這篇文章主要介紹了spring關(guān)于組件的注入及獲取流程場(chǎng)景分析,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2024-07-07