欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot 在項(xiàng)目啟動(dòng)之后執(zhí)行自定義方法的兩種方式小結(jié)

 更新時(shí)間:2021年09月18日 11:52:38   作者:Nick  
這篇文章主要介紹了SpringBoot 在項(xiàng)目啟動(dòng)之后執(zhí)行自定義方法的兩種方式小結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

SpringBoot 項(xiàng)目啟動(dòng)之后執(zhí)行自定義方法的兩種方式

在測(cè)試配置中心的配置時(shí),想在項(xiàng)目啟動(dòng)成功之后打印配置項(xiàng),然后需要執(zhí)行自定義的類(lèi)

一般項(xiàng)目中也會(huì)在這個(gè)地方進(jìn)行初始化數(shù)據(jù)的一些操作

方式一 實(shí)現(xiàn) CommandLineRunner 接口

自定義類(lèi)并實(shí)現(xiàn) CommandLineRunner 接口,實(shí)現(xiàn)run()方法,需要執(zhí)行的語(yǔ)句就放在 run() 方法中

例:

@Component
@Order(1)  // 控制類(lèi)執(zhí)行的順序越小越靠前
public class StartInitializer implements CommandLineRunner {
    @Override
    public void run(String... args) throws Exception {
        System.out.println("項(xiàng)目啟動(dòng),執(zhí)行 CommandLineRunner 實(shí)現(xiàn)類(lèi)的方法");
    }
}

方式二 實(shí)現(xiàn) ApplicationRunner 接口

自定義類(lèi)并實(shí)現(xiàn) ApplicationRunner 接口,實(shí)現(xiàn)run()方法,需要執(zhí)行的語(yǔ)句就放在 run() 方法中

例:

@Component
@Order(2) // 控制類(lèi)執(zhí)行的順序越小越靠前
public class AppInitializer implements ApplicationRunner {
    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println("項(xiàng)目啟動(dòng),執(zhí)行 ApplicationRunner 實(shí)現(xiàn)類(lèi)的方法");
    }
}

二者區(qū)別

區(qū)別在于實(shí)現(xiàn)方法 run() 中的參數(shù)類(lèi)型不一樣

實(shí)現(xiàn) ApplicationRunner 接口的 run() 方法參數(shù)類(lèi)型為: ApplicationArguments

實(shí)現(xiàn) CommandLineRunner 接口的 run() 方法參數(shù)類(lèi)型為: String...

實(shí)現(xiàn)效果

Springboot 項(xiàng)目啟動(dòng)后執(zhí)行某些自定義代碼

Springboot給我們提供了兩種“開(kāi)機(jī)啟動(dòng)”某些方法的方式:ApplicationRunner和CommandLineRunner。

這兩種方法提供的目的是為了滿(mǎn)足,在項(xiàng)目啟動(dòng)的時(shí)候立刻執(zhí)行某些方法。我們可以通過(guò)實(shí)現(xiàn)ApplicationRunner和CommandLineRunner,來(lái)實(shí)現(xiàn),他們都是在SpringApplication 執(zhí)行之后開(kāi)始執(zhí)行的。

CommandLineRunner接口可以用來(lái)接收字符串?dāng)?shù)組的命令行參數(shù),ApplicationRunner 是使用ApplicationArguments 用來(lái)接收參數(shù)的

代碼示例

@Component//被spring容器管理
@Order(1)//如果多個(gè)自定義ApplicationRunner,用來(lái)標(biāo)明執(zhí)行順序
public class MyApplicationRunner implements ApplicationRunner {
    @Override
    public void run(ApplicationArguments applicationArguments) throws Exception {
        System.out.println("-------------->" + "項(xiàng)目啟動(dòng),now=" + new Date());
        myTimer();
    }
    public static void myTimer(){
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                System.out.println("------定時(shí)任務(wù)--------");
            }
        }, 0, 1000);
    }
}

執(zhí)行結(jié)果

2018-02-08 14:10:16.490  INFO 10236 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8081 (http)
-------------->項(xiàng)目啟動(dòng),now=Thu Feb 08 14:10:16 CST 2018
------定時(shí)任務(wù)--------
2018-02-08 14:10:16.497  INFO 10236 --- [           main] com.mlxs.springboot01.web.MainApp        : Started MainApp in 5.595 seconds (JVM running for 6.334)
------定時(shí)任務(wù)--------
------定時(shí)任務(wù)--------
------定時(shí)任務(wù)--------
------定時(shí)任務(wù)--------
------定時(shí)任務(wù)--------
------定時(shí)任務(wù)--------

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論