關(guān)于CommandLineRunner的使用詳解
更新時間:2024年12月10日 10:01:10 作者:LBL_lin
本文介紹了如何在SpringBoot項目啟動時使用CommandLineRunner和ApplicationRunner接口進(jìn)行數(shù)據(jù)預(yù)加載或操作,通過實現(xiàn)這兩個接口,可以在項目啟動時執(zhí)行特定的任務(wù),同時,還展示了如何使用@Order注解來控制多個實現(xiàn)類的加載順序
背景
在項目啟動時需要做一些數(shù)據(jù)預(yù)加載或者某些操作,需要怎么辦呢,方法其實有好幾種,這里主要講一下SpringBoot提供的CommandLineRunner接口的使用。
案例說明以及實現(xiàn)
1.實現(xiàn)CommandLineRunner接口
- 定義一個類實現(xiàn)CommandLineRunner接口,模擬啟動項目時的預(yù)加載處理。
package com.lbl.run; import lombok.extern.slf4j.Slf4j; import org.springframework.boot.CommandLineRunner; import org.springframework.stereotype.Component; @Slf4j @Component public class WebStart implements CommandLineRunner { @Override public void run(String... args) throws Exception { log.info("------------- WebStart ---------------"); } }
- 啟動類
package com.lbl; import lombok.extern.slf4j.Slf4j; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @Slf4j @SpringBootApplication public class SpringbootDemoApplication { public static void main(String[] args) { log.info("------------- before ---------------"); SpringApplication.run(SpringbootDemoApplication.class, args); log.info("------------- after ---------------"); } }
- 啟動啟動類,查看日志的打印
2.加載的順序
- 如果有多個實現(xiàn)類,我們可以使用@Order()注解控制它們的加載順序,數(shù)字越小加載越早。
- 現(xiàn)在創(chuàng)建多一個CommandLineRunnerd的實現(xiàn)類,給它們加上@Order()注解。
package com.lbl.run; import lombok.extern.slf4j.Slf4j; import org.springframework.boot.CommandLineRunner; import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; @Slf4j @Component @Order(2) public class WebStart implements CommandLineRunner { @Override public void run(String... args) throws Exception { log.info("------------- WebStart ---------------"); } }
package com.lbl.run; import lombok.extern.slf4j.Slf4j; import org.springframework.boot.CommandLineRunner; import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; @Slf4j @Component @Order(1) public class WebStart2 implements CommandLineRunner { @Override public void run(String... args) throws Exception { log.info("------------- WebStart2 ---------------"); } }
- 啟動啟動類,查看日志的打印
3.擴(kuò)展-ApplicationRunner
- 除了實現(xiàn)CommandLineRunner接口可以完成項目啟動時的預(yù)加載動作,還有ApplicationRunner也能實現(xiàn)同樣的功能,并且在不設(shè)置@Order()的情況下,ApplicationRunner的優(yōu)先級大于CommandLineRunner。
package com.lbl.run; import lombok.extern.slf4j.Slf4j; import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationRunner; import org.springframework.stereotype.Component; @Slf4j @Component public class WebStart3 implements ApplicationRunner { @Override public void run(ApplicationArguments args) throws Exception { log.info("------------- WebStart3 ---------------"); } }
此時注掉前面兩個實現(xiàn)類的@Order()注解
- 啟動實現(xiàn)類,查看日志的打印
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
java數(shù)據(jù)庫批量插入數(shù)據(jù)的實現(xiàn)
本文主要介紹了java數(shù)據(jù)庫批量插入數(shù)據(jù)的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-05-05Springbootadmin與security沖突問題及解決
這篇文章主要介紹了Springbootadmin與security沖突問題及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-08-08Java實現(xiàn)漢字轉(zhuǎn)全拼音的方法總結(jié)
在軟件開發(fā)中,經(jīng)常會遇到需要將漢字轉(zhuǎn)換成拼音的場景,比如在搜索引擎優(yōu)化、數(shù)據(jù)存儲、國際化等方面,Java作為一種廣泛使用的編程語言,提供了多種方法來實現(xiàn)漢字到拼音的轉(zhuǎn)換,本文將詳細(xì)介紹幾種常用的Java漢字轉(zhuǎn)全拼音的方法,并提供具體的代碼示例和步驟2024-12-12Java獲取本機(jī)IP地址的方法代碼示例(內(nèi)網(wǎng)、公網(wǎng))
在IT領(lǐng)域獲取本機(jī)IP地址是一項基礎(chǔ)但重要的任務(wù),特別是在網(wǎng)絡(luò)編程、遠(yuǎn)程協(xié)作和設(shè)備通信中,這篇文章主要給大家介紹了關(guān)于Java獲取本機(jī)IP地址的方法(內(nèi)網(wǎng)、公網(wǎng)),需要的朋友可以參考下2024-07-07解析SpringBoot整合SpringDataRedis的過程
這篇文章主要介紹了SpringBoot整合SpringDataRedis的過程,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-06-06