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

解讀CommandLineRunner或者ApplicationRunner接口

 更新時(shí)間:2023年02月13日 10:06:47   作者:二月_春風(fēng)  
這篇文章主要介紹了解讀CommandLineRunner或者ApplicationRunner接口的使用,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

前言

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

CommandLineRunner接口

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容器中時(shí)執(zhí)行其run方法。多個(gè)CommandLineRunner可以被同時(shí)執(zhí)行在同一個(gè)spring上下文中并且執(zhí)行順序是以order注解的參數(shù)順序一致。

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

如果你需要訪問ApplicationArguments去替換掉字符串?dāng)?shù)組,可以考慮使用ApplicationRunner類。

先看一個(gè)demo:

定義一個(gè)ServerStartedReport實(shí)現(xiàn)CommandLineRunner,并納入到srping容器中進(jìn)行處理

import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
 
import java.time.LocalDateTime;
 
@Component
public class ServerStartedReport implements CommandLineRunner{
    @Override
    public void run(String... args) throws Exception {
        System.out.println("===========ServerStartedReport啟動====="+ LocalDateTime.now());
    }
}

定義一個(gè)ServerSuccessReport實(shí)現(xiàn)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("=====應(yīng)用已經(jīng)成功啟動====="+ 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"));
    }
}

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

打印結(jié)果

ApplicationRunner接口

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

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

可以接收--foo=bar這樣的參數(shù)。

  • --getOptionNames()方法可以得到foo這樣的key的集合。
  • --getOptionValues(String name)方法可以得到bar這樣的集合的value。

看一個(gè)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);
    }
}

配置參數(shù)啟動:

打印結(jié)果:

總結(jié)

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

相關(guān)文章

  • Java實(shí)現(xiàn)隊(duì)列的三種方法集合

    Java實(shí)現(xiàn)隊(duì)列的三種方法集合

    這篇文章主要介紹了Java實(shí)現(xiàn)隊(duì)列的三種方法集合,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-09-09
  • springboot配置resilience4j全過程

    springboot配置resilience4j全過程

    這篇文章主要介紹了springboot配置resilience4j全過程,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-05-05
  • gson對象序列化的示例

    gson對象序列化的示例

    本文介紹如何將Java對象序列化為Json文件,然后讀取該Json文件讀取回Java對象。在下面的示例中,我們創(chuàng)建了一個(gè)Student類。然后生成一個(gè)student.json文件,該文件將具有Student對象的json數(shù)據(jù)。
    2020-11-11
  • java批量修改文件名的實(shí)現(xiàn)方法

    java批量修改文件名的實(shí)現(xiàn)方法

    這篇文章主要介紹了 java批量修改文件名的實(shí)現(xiàn)方法的相關(guān)資料,實(shí)現(xiàn)批量修改文件下的所有文件的文件名,具有一定的參考價(jià)值,需要的朋友可以參考下
    2017-07-07
  • SpringBoot實(shí)現(xiàn)指標(biāo)監(jiān)控

    SpringBoot實(shí)現(xiàn)指標(biāo)監(jiān)控

    這篇文章主要介紹了SpringBoot實(shí)現(xiàn)指標(biāo)監(jiān)控方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-05-05
  • 關(guān)于Java中的mysql時(shí)區(qū)問題詳解

    關(guān)于Java中的mysql時(shí)區(qū)問題詳解

    這篇文章主要給大家介紹了關(guān)于Java中mysql時(shí)區(qū)問題的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Java具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-05-05
  • RocketMQ消息重試機(jī)制原理分析講解

    RocketMQ消息重試機(jī)制原理分析講解

    這篇文章主要介紹了RocketMQ消息重試機(jī)制,消息的發(fā)送和消費(fèi)并不是百分百成功的,在出現(xiàn)消息推送失敗時(shí),RocketMQ有何補(bǔ)償方式來進(jìn)行消息重試呢?這是我們今天要一起學(xué)習(xí)的點(diǎn)
    2023-02-02
  • Java連接Linux服務(wù)器過程分析(附代碼)

    Java連接Linux服務(wù)器過程分析(附代碼)

    這篇文章主要介紹了Java連接Linux服務(wù)器過程分析(附代碼),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-12-12
  • Java使用JDBC連接數(shù)據(jù)庫的詳細(xì)步驟

    Java使用JDBC連接數(shù)據(jù)庫的詳細(xì)步驟

    本文詳細(xì)講解了Java使用JDBC連接數(shù)據(jù)庫的詳細(xì)步驟,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-01-01
  • springboot整合Shiro的步驟

    springboot整合Shiro的步驟

    這篇文章主要介紹了springboot整合Shiro的步驟,幫助大家更好的理解和使用springboot框架,感興趣的朋友可以了解下
    2021-01-01

最新評論