關(guān)于CommandLineRunner的使用詳解
背景
在項(xiàng)目啟動(dòng)時(shí)需要做一些數(shù)據(jù)預(yù)加載或者某些操作,需要怎么辦呢,方法其實(shí)有好幾種,這里主要講一下SpringBoot提供的CommandLineRunner接口的使用。
案例說(shuō)明以及實(shí)現(xiàn)
1.實(shí)現(xiàn)CommandLineRunner接口
- 定義一個(gè)類實(shí)現(xiàn)CommandLineRunner接口,模擬啟動(dòng)項(xiàng)目時(shí)的預(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 ---------------"); } }
- 啟動(dòng)類
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 ---------------"); } }
- 啟動(dòng)啟動(dòng)類,查看日志的打印
2.加載的順序
- 如果有多個(gè)實(shí)現(xiàn)類,我們可以使用@Order()注解控制它們的加載順序,數(shù)字越小加載越早。
- 現(xiàn)在創(chuàng)建多一個(gè)CommandLineRunnerd的實(shí)現(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 ---------------"); } }
- 啟動(dòng)啟動(dòng)類,查看日志的打印
3.擴(kuò)展-ApplicationRunner
- 除了實(shí)現(xiàn)CommandLineRunner接口可以完成項(xiàng)目啟動(dòng)時(shí)的預(yù)加載動(dòng)作,還有ApplicationRunner也能實(shí)現(xiàn)同樣的功能,并且在不設(shè)置@Order()的情況下,ApplicationRunner的優(yōu)先級(jí)大于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 ---------------"); } }
此時(shí)注掉前面兩個(gè)實(shí)現(xiàn)類的@Order()注解
- 啟動(dòng)實(shí)現(xiàn)類,查看日志的打印
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- SpringBoot CommandLineRunner的異步任務(wù)機(jī)制使用
- SpringBoot使用CommandLineRunner和ApplicationRunner執(zhí)行初始化業(yè)務(wù)方式
- 使用SpringBoot的CommandLineRunner遇到的坑及解決
- SpringBoot使用CommandLineRunner接口完成資源初始化方式
- springboot使用CommandLineRunner解決項(xiàng)目啟動(dòng)時(shí)初始化資源的操作
- Spring Boot 啟動(dòng)加載數(shù)據(jù) CommandLineRunner的使用
相關(guān)文章
java數(shù)據(jù)庫(kù)批量插入數(shù)據(jù)的實(shí)現(xiàn)
本文主要介紹了java數(shù)據(jù)庫(kù)批量插入數(shù)據(jù)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-05-05Springbootadmin與security沖突問(wèn)題及解決
這篇文章主要介紹了Springbootadmin與security沖突問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08Java實(shí)現(xiàn)漢字轉(zhuǎn)全拼音的方法總結(jié)
在軟件開(kāi)發(fā)中,經(jīng)常會(huì)遇到需要將漢字轉(zhuǎn)換成拼音的場(chǎng)景,比如在搜索引擎優(yōu)化、數(shù)據(jù)存儲(chǔ)、國(guó)際化等方面,Java作為一種廣泛使用的編程語(yǔ)言,提供了多種方法來(lái)實(shí)現(xiàn)漢字到拼音的轉(zhuǎn)換,本文將詳細(xì)介紹幾種常用的Java漢字轉(zhuǎn)全拼音的方法,并提供具體的代碼示例和步驟2024-12-12Java獲取本機(jī)IP地址的方法代碼示例(內(nèi)網(wǎng)、公網(wǎng))
在IT領(lǐng)域獲取本機(jī)IP地址是一項(xiàng)基礎(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的過(guò)程
這篇文章主要介紹了SpringBoot整合SpringDataRedis的過(guò)程,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06Jpa使用Page和Pageable分頁(yè)遇到的問(wèn)題及解決
這篇文章主要介紹了Jpa使用Page和Pageable分頁(yè)遇到的問(wèn)題及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07