溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

CommandLineRunner與ApplicationRunner是什么

發(fā)布時間:2020-10-29 10:13:51 來源:億速云 閱讀:383 作者:小新 欄目:編程語言

小編給大家分享一下CommandLineRunner與ApplicationRunner是什么,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

CommandLineRunner和ApplicationRunner是Spring Boot所提供的接口,他們都有一個run()方法。所有實現(xiàn)他們的Bean都會在Spring Boot服務啟動之后自動地被調用。

由于這個特性,它們是一個理想地方去做一些初始化的工作,或者寫一些測試代碼。

CommandLineRunner

使用Application實現(xiàn)

在我們新建好工程后,為了簡單我們直接使用Application類實現(xiàn)CommandLineRunner接口,這個類的注解@SpringBootApplication會為我們自動配置。

package cn.examplecode.sb2runner;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Sb2runnerApplication implements CommandLineRunner {

    private static Logger logger = LoggerFactory.getLogger(Sb2runnerApplication.class);

    public static void main(String[] args) {
        SpringApplication.run(Sb2runnerApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        logger.info("服務已啟動,執(zhí)行command line runner。");

        for (int i = 0; i < args.length; ++i) {
            logger.info("args[{}]: {}", i, args[i]);
        }
    }
}

接下來我們直接啟動服務,查看日志如下,發(fā)現(xiàn)run()方法被正常地執(zhí)行了:

Tomcat started on port(s): 8080 (http) with context path ''
Started Sb2runnerApplication in 2.204 seconds (JVM running for 3.161)
服務已啟動,執(zhí)行command line runner。

參數(shù)傳遞

run()方法有個可變參數(shù)args,這個參數(shù)是用來接收命令行參數(shù)的,我們下面來加入?yún)?shù)來測試一下:

CommandLineRunner與ApplicationRunner是什么

然后重啟服務,觀察日志,可以看到參數(shù)被正常地接收到了:

Tomcat started on port(s): 8080 (http) with context path ''
Started Sb2runnerApplication in 1.888 seconds (JVM running for 2.41)
服務已啟動,執(zhí)行command line runner。
args[0]: --param=sth

命令行參數(shù)傳遞

之前我們說過使用Spring Boot的一大優(yōu)勢就是可以將工程直接打包成一個jar包而不需要單獨部署。打包成jar包后可以直接執(zhí)行該jar包進行服務的啟動,這樣在執(zhí)行jar包時我們就可以傳入命令行參數(shù),讓CommandLineRunner接收參數(shù)。

這種場景在服務器上特別常用。比如我們想執(zhí)行某個操作,又不想對外部暴露,此時可以使用CommandLineRunner作為該操作的入口。

下面我們就打成jar包來演示一下。

  1. 進入終端界面,開始打包

CommandLineRunner與ApplicationRunner是什么

  1. 打包完成后,執(zhí)行該jar包,記得先把IDE的服務停掉。

CommandLineRunner與ApplicationRunner是什么

可以從日志中看到我們也正常地獲取到了參數(shù)。通過傳遞參數(shù),在業(yè)務邏輯上我們可以根據(jù)不同的參數(shù)而執(zhí)行不同的操作。

上面我們提到的只是一個CommandLineRunner,如果我們有多個CommandLineRunner怎么辦呢?怎么控制它們執(zhí)行的順序呢?

下面我們就來介紹如何指定執(zhí)行的順序。

指定執(zhí)行順序

Spring Boot為我們提供了一個注解"@Order",可以用來指定執(zhí)行的順序,比如我們工程里面有三個CommandLineRunner:

@Component
@Order(1)
public class CommandRunner1 implements CommandLineRunner {

    private static Logger logger = LoggerFactory.getLogger(CommandRunner1.class);

    @Override
    public void run(String... args) throws Exception {
        logger.info("執(zhí)行第一個command line runner...");
    }

}


@Component
@Order(2)
public class CommandRunner2 implements CommandLineRunner {

    private static Logger logger = LoggerFactory.getLogger(CommandRunner2.class);

    @Override
    public void run(String... args) throws Exception {
        logger.info("執(zhí)行第二個command line runner...");
    }
    
}

@Component
@Order(3)
public class CommandRunner3 implements CommandLineRunner {

    private static Logger logger = LoggerFactory.getLogger(CommandRunner3.class);

    @Override
    public void run(String... args) throws Exception {
        logger.info("執(zhí)行第三個command line runner...");
    }
    
}

我們可以在該類的上面直接加入@Order注解,然后Spring Boot就會按照我們注解指定的順序從小到大的執(zhí)行了。很簡單,是不是?

Tomcat started on port(s): 8080 (http) with context path ''
Started Sb2runnerApplication in 1.764 seconds (JVM running for 2.292)
執(zhí)行第一個command line runner...
執(zhí)行第二個command line runner...
執(zhí)行第三個command line runner...

ApplicationRunner

ApplicationRunner與CommandLineRunner做的事情是一樣的,也是在服務啟動之后其run()方法會被自動地調用,唯一不同的是ApplicationRunner會封裝命令行參數(shù),可以很方便地獲取到命令行參數(shù)和參數(shù)值。

@Component
public class ApplicationRunner1 implements ApplicationRunner {

    private static Logger logger = LoggerFactory.getLogger(ApplicationRunner1.class);

    @Override
    public void run(ApplicationArguments args) throws Exception {
        logger.info("執(zhí)行application runner...");
        logger.info("獲取到參數(shù): " + args.getOptionValues("param"));
    }
}

執(zhí)行結果:

CommandLineRunner與ApplicationRunner是什么

我們可以發(fā)現(xiàn),通過run()方法的參數(shù)ApplicationArguments可以很方便地獲取到命令行參數(shù)的值。

所以如果你的工程需要獲取命令行參數(shù)的話,建議你使用ApplicationRunner。

總結

無論是CommandLineRunner還是ApplicationRunner,它們的目的都是在服務啟動之后執(zhí)行一些操作。如果需要獲取命令行參數(shù)時則建議使用ApplicationRunner。

另一種場景是我們在服務器上需要執(zhí)行某個操作,比如修正數(shù)據(jù)庫用戶的數(shù)據(jù),而又找不到合適的執(zhí)行入口,那么這就是它們理想的使用場景了。

以上是CommandLineRunner與ApplicationRunner是什么的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業(yè)資訊頻道!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內容。

AI