關(guān)于CommandLineRunner的使用詳解
背景
在項目啟動時需要做一些數(shù)據(jù)預(yù)加載或者某些操作,需要怎么辦呢,方法其實(shí)有好幾種,這里主要講一下SpringBoot提供的CommandLineRunner接口的使用。
案例說明以及實(shí)現(xiàn)
1.實(shí)現(xiàn)CommandLineRunner接口
- 定義一個類實(shí)現(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.加載的順序
- 如果有多個實(shí)現(xiàn)類,我們可以使用@Order()注解控制它們的加載順序,數(shù)字越小加載越早。
- 現(xiàn)在創(chuàng)建多一個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 ---------------");
}
}- 啟動啟動類,查看日志的打印

3.擴(kuò)展-ApplicationRunner
- 除了實(shí)現(xiàn)CommandLineRunner接口可以完成項目啟動時的預(yù)加載動作,還有ApplicationRunner也能實(shí)現(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 ---------------");
}
}此時注掉前面兩個實(shí)現(xiàn)類的@Order()注解
- 啟動實(shí)現(xiàn)類,查看日志的打印

總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java遞歸調(diào)用如何實(shí)現(xiàn)數(shù)字的逆序輸出方式
這篇文章主要介紹了Java遞歸調(diào)用如何實(shí)現(xiàn)數(shù)字的逆序輸出方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-04-04
java JTree JCheckBox樹復(fù)選框詳解
這篇文章主要為大家詳細(xì)介紹了java JTree JCheckBox樹復(fù)選框的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-11-11
java Hibernate多對多映射詳解及實(shí)例代碼
這篇文章主要介紹了java Hibernate多對多映射詳解及實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-01-01
Mybatis查不到數(shù)據(jù)查詢返回Null問題
mybatis突然查不到數(shù)據(jù),查詢返回的都是Null,但是 select count(*) from xxx查詢數(shù)量,返回卻是正常的。好多朋友遇到這樣的問題不知所措,下面小編通過本教程簡單給大家說明下2016-08-08
Spring AOP攔截-三種方式實(shí)現(xiàn)自動代理詳解
這篇文章主要介紹了Spring AOP攔截-三種方式實(shí)現(xiàn)自動代理詳解,還是比較不錯的,這里分享給大家,供需要的朋友參考。2017-11-11
Java實(shí)現(xiàn)Word/Pdf/TXT轉(zhuǎn)html的實(shí)例代碼
本文主要介紹了Java實(shí)現(xiàn)Word/Pdf/TXT轉(zhuǎn)html的實(shí)例代碼,代碼簡單易懂,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2020-02-02

