Spring Boot 啟動(dòng)加載數(shù)據(jù) CommandLineRunner的使用
實(shí)際應(yīng)用中,我們會(huì)有在項(xiàng)目服務(wù)啟動(dòng)的時(shí)候就去加載一些數(shù)據(jù)或做一些事情這樣的需求。
為了解決這樣的問(wèn)題,spring Boot 為我們提供了一個(gè)方法,通過(guò)實(shí)現(xiàn)接口 CommandLineRunner 來(lái)實(shí)現(xiàn)。
很簡(jiǎn)單,只需要一個(gè)類就可以,無(wú)需其他配置。
創(chuàng)建實(shí)現(xiàn)接口 CommandLineRunner 的類
package org.springboot.sample.runner;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
/**
* 服務(wù)啟動(dòng)執(zhí)行
*
* @author 單紅宇(365384722)
* @create 2016年1月9日
*/
@Component
public class MyStartupRunner1 implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
System.out.println(">>>>>>>>>>>>>>>服務(wù)啟動(dòng)執(zhí)行,執(zhí)行加載數(shù)據(jù)等操作<<<<<<<<<<<<<");
}
}
Spring Boot應(yīng)用程序在啟動(dòng)后,會(huì)遍歷CommandLineRunner接口的實(shí)例并運(yùn)行它們的run方法。也可以利用@Order注解(或者實(shí)現(xiàn)Order接口)來(lái)規(guī)定所有CommandLineRunner實(shí)例的運(yùn)行順序。
如下我們使用@Order 注解來(lái)定義執(zhí)行順序。
package org.springboot.sample.runner;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
/**
* 服務(wù)啟動(dòng)執(zhí)行
*
* @author 單紅宇(365384722)
* @create 2016年1月9日
*/
@Component
@Order(value=2)
public class MyStartupRunner1 implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
System.out.println(">>>>>>>>>>>>>>>服務(wù)啟動(dòng)執(zhí)行,執(zhí)行加載數(shù)據(jù)等操作 11111111 <<<<<<<<<<<<<");
}
}
package org.springboot.sample.runner;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
/**
* 服務(wù)啟動(dòng)執(zhí)行
*
* @author 單紅宇(365384722)
* @create 2016年1月9日
*/
@Component
@Order(value=1)
public class MyStartupRunner2 implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
System.out.println(">>>>>>>>>>>>>>>服務(wù)啟動(dòng)執(zhí)行,執(zhí)行加載數(shù)據(jù)等操作 22222222 <<<<<<<<<<<<<");
}
}
啟動(dòng)程序后,控制臺(tái)輸出結(jié)果為:
>>>>>>>>>>>>>>>服務(wù)啟動(dòng)執(zhí)行,執(zhí)行加載數(shù)據(jù)等操作 22222222 <<<<<<<<<<<<<
>>>>>>>>>>>>>>>服務(wù)啟動(dòng)執(zhí)行,執(zhí)行加載數(shù)據(jù)等操作 11111111 <<<<<<<<<<<<<
根據(jù)控制臺(tái)結(jié)果可判斷,@Order 注解的執(zhí)行優(yōu)先級(jí)是按value值從小到大順序。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
java圖論弗洛伊德和迪杰斯特拉算法解決最短路徑問(wèn)題
這篇文章主要為大家介紹了java圖論弗洛伊德算法和迪杰斯特拉算法解決最短路徑的問(wèn)題示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助2021-11-11
Java實(shí)現(xiàn)“年-月-日 上午/下午時(shí):分:秒”的簡(jiǎn)單代碼
當(dāng)前的日期輸出的方法有很多,本文為大家介紹下在java中是如何實(shí)現(xiàn)“年-月-日 上午/下午時(shí):分:秒”,感興趣的朋友不妨參考下2015-08-08
詳解SpringBoot中的參數(shù)校驗(yàn)(項(xiàng)目實(shí)戰(zhàn))
這篇文章主要介紹了SpringBoot中的參數(shù)校驗(yàn),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07
SpringBoot?Test的webEnvironment源碼解讀
這篇文章主要為大家介紹了SpringBoot?Test的webEnvironment源碼解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09
淺談Zookeeper開(kāi)源客戶端框架Curator
這篇文章主要介紹了淺談Zookeeper開(kāi)源客戶端框架Curator的相關(guān)內(nèi)容,具有一定參考價(jià)值,需要的朋友可以了解下。2017-10-10
SpringBoot實(shí)現(xiàn)微信支付接口調(diào)用及回調(diào)函數(shù)(商戶參數(shù)獲取)
本文詳細(xì)介紹了使用SpringBoot實(shí)現(xiàn)微信支付接口調(diào)用及回調(diào)函數(shù)的步驟,提供了代碼實(shí)現(xiàn)的具體步驟和工具類的創(chuàng)建,感興趣的朋友跟隨小編一起看看吧2024-11-11

