成人无码视频,亚洲精品久久久久av无码,午夜精品久久久久久毛片,亚洲 中文字幕 日韩 无码

資訊專欄INFORMATION COLUMN

CommandLineRunner與ApplicationRunner接口的使用及源碼解析

tylin / 1319人閱讀

摘要:實例定義一個實現,并納入到容器中進行處理啟動定義一個實現,并納入到容器處理應用已經成功啟動啟動類測試,也可以直接在容器訪問該值,配置參數,然后執(zhí)行啟動類打印結果接口發(fā)現二者的官方一樣,區(qū)別在于接收的參數不一樣。

引言

我們在使用SpringBoot搭建項目的時候,如果希望在項目啟動完成之前,能夠初始化一些操作,針對這種需求,可以考慮實現如下兩個接口(任一個都可以)

org.springframework.boot.CommandLineRunner
org.springframework.boot.ApplicationRunner

CommandLineRunner、ApplicationRunner 接口是在容器啟動成功后的最后一步回調(類似開機自啟動)。

CommandLineRunner接口

官方doc: Interface used to indicate that a bean should run when it is contained within a SpringApplication. Multiple CommandLineRunner beans can be defined within the same application context and can be ordered using the Ordered interface or Order @Order annotation.

接口被用作將其加入spring容器中時執(zhí)行其run方法。多個CommandLineRunner可以被同時執(zhí)行在同一個spring上下文中并且執(zhí)行順序是以order注解的參數順序一致。

If you need access to ApplicationArguments instead of the raw String array consider using ApplicationRunner.

如果你需要訪問ApplicationArguments去替換掉字符串數組,可以考慮使用ApplicationRunner類。

實例demo

定義一個ServerStartedReport實現CommandLineRunner,并納入到srping容器中進行處理

import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;

@Order(2)
@Component
public class ServerStartedReport implements CommandLineRunner{
    @Override
    public void run(String... args) throws Exception {
        System.out.println("===========ServerStartedReport啟動====="+ LocalDateTime.now());
    }
}

定義一個ServerSuccessReport實現CommandLineRunner,并納入到spring容器處理

import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import java.util.Arrays;

@Order(1)
@Component
public class ServerSuccessReport implements CommandLineRunner{
    @Override
    public void run(String... args) throws Exception {
        System.out.println("=====應用已經成功啟動====="+ Arrays.asList(args));
    }
}

啟動類測試,也可以直接在spring容器訪問該值,

import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        ConfigurableApplicationContext context =SpringApplication.run(Application.class,args);
        ApplicationArguments applicationArguments = context.getBean(ApplicationArguments.class);
        System.out.println("============");
        System.out.println("name="+applicationArguments.getOptionNames());
        System.out.println("values===="+applicationArguments.getOptionValues("developer.name"));
    }
}

配置參數,然后執(zhí)行啟動類

打印結果

ApplicationRunner接口

發(fā)現二者的官方javadoc一樣,區(qū)別在于接收的參數不一樣。CommandLineRunner的參數是最原始的參數,沒有做任何處理。ApplicationRunner的參數是ApplicationArguments,是對原始參數做了進一步的封裝。

ApplicationArguments是對參數(main方法)做了進一步的處理,可以解析--name=value的,我們就可以通過name來獲取value(而CommandLineRunner只是獲取--name=value)

可以接收--foo=bar這樣的參數。

getOptionNames()方法可以得到foo這樣的key的集合。

getOptionValues(String name)方法可以得到bar這樣的集合的value。

實例demo

定義MyApplicationRunner類繼承ApplicationRunner接口,

import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import java.util.Arrays;

@Component
public class MyApplicationRunner implements ApplicationRunner{
    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println("===MyApplicationRunner==="+ Arrays.asList(args.getSourceArgs()));
        System.out.println("===getOptionNames========"+args.getOptionNames());
        System.out.println("===getOptionValues======="+args.getOptionValues("foo"));
        System.out.println("==getOptionValues========"+args.getOptionValues("developer.name"));
    }
}

啟動類

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

配置參數啟動

打印結果

總結

用戶使用CommandLineRunner或者ApplicationRunner接口均可實現應用啟動初始化某些功能的需求,如果希望對參數有更多的操作,則可以選擇實現ApplicationRunner接口。

擴展閱讀 CommandLineRunner、ApplicationRunner執(zhí)行流程源碼分析

用戶只要實現這兩個接口,其中的run方法就會在項目啟動時候被自動調用,那么究竟是在什么時候調用的呢?下面可以看一下Application的啟動流程

SpringApplication.run(args)

this.afterRefresh(context, applicationArguments)方法

跟蹤context.getBeansOfType()方法,具體實現在類DefaultListableBeanFactory中

總結:通過以上分析可知,實現這兩個接口的類,在ApplicationContext.run()方法里被執(zhí)行

文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規(guī)行為,您可以聯系管理員刪除。

轉載請注明本文地址:http://m.hztianpu.com/yun/7401.html

相關文章

  • 如何在SpringBoot啟動時執(zhí)行初始化操作,兩個簡單接口就可以實現

    摘要:中有兩個接口能實現該功能和。首先了解一下的基本用法,可以在系統(tǒng)啟動后執(zhí)行里面的方法執(zhí)行數據初始化如果有多個類的話也可以通過注解指定每個類的執(zhí)行順序。 (一)概述 最...

    wuyangnju 評論0 收藏0
  • spring boot學習(4): 命令行啟動

    摘要:在使用構建應用啟動時,我們在工作中都是通過命令行來啟動應用,有時候會需要一些特定的參數以在應用啟動時,做一些初始化的操作。 在使用spring boot 構建應用啟動時,我們在工作中都是通過命令行來啟動應用,有時候會需要一些特定的參數以在應用啟動時,做一些初始化的操作。 spring boot 提供了 CommandLineRunner 和 ApplicationRunner 這兩個接...

    Binguner 評論0 收藏0
  • Spring Boot 2 - 使用CommandLineRunnerApplicationRun

    摘要:命令行參數傳遞之前我們說過使用的一大優(yōu)勢就是可以將工程直接打包成一個包而不需要單獨部署。執(zhí)行獲取到參數執(zhí)行結果我們可以發(fā)現,通過方法的參數可以很方便地獲取到命令行參數的值。如果需要獲取命令行參數時則建議使用。 本篇文章我們將探討CommandLineRunner和ApplicationRunner的使用。 在閱讀本篇文章之前,你可以新建一個工程,寫一些關于本篇內容代碼,這樣會加深你對本...

    alogy 評論0 收藏0
  • Spring Boot啟動退出加載項

    摘要:在一個初春的下午,甲跟我說,要在啟動服務的時候,設置表自增的起始值。寫完啟動項,那么再把退出也說一下每一個都應該向注冊一個鉤子函數來確保能優(yōu)雅地關閉。后面退出部分翻譯地磕磕碰碰的,有不對的地方歡迎指正。原創(chuàng)不易,感謝支持。 在一個初春的下午,甲跟我說,要在Spring Boot啟動服務的時候,設置表自增的起始值。于是我用屁股想了一下,不就是在main方法里折騰嘛。后來實際操作了一把,發(fā)...

    suosuopuo 評論0 收藏0
  • 最渣 Spring Boot 文章

    摘要:如刪除臨時文件,清除緩存信息,讀取配置文件信息,數據庫連接等。提供的接口也可以滿足該業(yè)務場景。不同點中方法的參數為,而接口中方法的參數為數組。 spring-boot-starter-parent Maven的用戶可以通過繼承spring-boot-starter-parent項目來獲得一些合理的默認配置。這個parent提供了以下特性: 默認使用Java 8 使用UTF-8編碼 一...

    yanest 評論0 收藏0

發(fā)表評論

0條評論

閱讀需要支付1元查看
<