JAVA專題----Spring Boot學(xué)習(xí)--項(xiàng)目啟動(dòng)時(shí)執(zhí)行特定方法

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

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

CommandLineRunner接口可以用來接收字符串?dāng)?shù)組的命令行參數(shù),ApplicationRunner 是使用ApplicationArguments 用來接收參數(shù)的,貌似后者更牛逼一些。

先看看CommandLineRunner :

package com.springboot.study;

import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

/**
 * Created by pangkunkun on 2017/9/3.
 */
@Component
public class MyCommandLineRunner implements CommandLineRunner{

    @Override
    public void run(String... var1) throws Exception{
        System.out.println("This will be execute when the project was started!");
    }
}

ApplicationRunner :

package com.springboot.study;

import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

/**
 * Created by pangkunkun on 2017/9/3.
 */
@Component
public class MyApplicationRunner implements ApplicationRunner {

    @Override
    public void run(ApplicationArguments var1) throws Exception{
        System.out.println("MyApplicationRunner class will be execute when the project was started!");
    }

}

這兩種方式的實(shí)現(xiàn)都很簡單,直接實(shí)現(xiàn)了相應(yīng)的接口就可以了。記得在類上加@Component注解。

如果想要指定啟動(dòng)方法執(zhí)行的順序,可以通過實(shí)現(xiàn)org.springframework.core.Ordered接口或者使用org.springframework.core.annotation.Order注解來實(shí)現(xiàn)。

這里我們以ApplicationRunner 為例來分別實(shí)現(xiàn)。

Ordered接口:

package com.springboot.study;

import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;

/**
 * Created by pangkunkun on 2017/9/3.
 */
@Component
public class MyApplicationRunner implements ApplicationRunner,Ordered{


    @Override
    public int getOrder(){
        return 1;//通過設(shè)置這里的數(shù)字來知道指定順序
    }

    @Override
    public void run(ApplicationArguments var1) throws Exception{
        System.out.println("MyApplicationRunner1!");
    }

}

Order注解實(shí)現(xiàn)方式:

package com.springboot.study;

import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * Created by pangkunkun on 2017/9/3.
 * 這里通過設(shè)定value的值來指定執(zhí)行順序
 */
@Component
@Order(value = 1)
public class MyApplicationRunner implements ApplicationRunner{

    @Override
    public void run(ApplicationArguments var1) throws Exception{
        System.out.println("MyApplicationRunner1!");
    }

}

這里不列出其他對比方法了,自己執(zhí)行下就好。

https://blog.csdn.net/qq_35981283/article/details/77826537

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容