SpringBoot2-使用CommandLineRunner與ApplicationRun

本篇文章我們將探討CommandLineRunner和ApplicationRunner的使用。

創(chuàng)新互聯是一家專注于網站制作、做網站與策劃設計,蔡甸網站建設哪家好?創(chuàng)新互聯做網站,專注于網站建設十多年,網設計領域的專業(yè)建站公司;建站業(yè)務涵蓋:蔡甸等地區(qū)。蔡甸做網站價格咨詢:18980820575

在閱讀本篇文章之前,你可以新建一個工程,寫一些關于本篇內容代碼,這樣會加深你對本文內容的理解,關于如何快速創(chuàng)建新工程,可以參考我的這篇博客:

Spring Boot 2 - 創(chuàng)建新工程

概述

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

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

CommandLineRunner

使用Application實現

在我們新建好工程后,為了簡單我們直接使用Application類實現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ā)現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。

參數傳遞

run()方法有個可變參數args,這個參數是用來接收命令行參數的,我們下面來加入參數來測試一下:
Spring Boot 2 - 使用CommandLineRunner與ApplicationRun

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

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

命令行參數傳遞

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

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

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

  1. 進入終端界面,開始打包
    Spring Boot 2 - 使用CommandLineRunner與ApplicationRun

  2. 打包完成后,執(zhí)行該jar包,記得先把IDE的服務停掉。
    Spring Boot 2 - 使用CommandLineRunner與ApplicationRun

可以從日志中看到我們也正常地獲取到了參數。通過傳遞參數,在業(yè)務邏輯上我們可以根據不同的參數而執(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會封裝命令行參數,可以很方便地獲取到命令行參數和參數值。

@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("獲取到參數: " + args.getOptionValues("param"));
    }
}

執(zhí)行結果:
Spring Boot 2 - 使用CommandLineRunner與ApplicationRun

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

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

總結

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

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

我的博客中其他關于Spring Boot的所有文章可以點擊這里找到,歡迎關注!

如果有問題可以留言,或者給我發(fā)郵件lloyd@examplecode.cn,期待我們共同學習與成長!

新聞標題:SpringBoot2-使用CommandLineRunner與ApplicationRun
URL網址:http://muchs.cn/article18/gdisdp.html

成都網站建設公司_創(chuàng)新互聯,為您提供網頁設計公司、商城網站虛擬主機、定制開發(fā)、建站公司、外貿網站建設

廣告

聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯

網站建設網站維護公司