最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

spring boot 命令行啟動的方式

 更新時間:2019年03月17日 10:02:11   作者:posuoren  
這篇文章主要介紹了spring boot 命令行啟動的方式,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

在使用spring boot 構(gòu)建應(yīng)用啟動時,我們在工作中都是通過命令行來啟動應(yīng)用,有時候會需要一些特定的參數(shù)以在應(yīng)用啟動時,做一些初始化的操作。

spring boot 提供了 CommandLineRunner 和 ApplicationRunner 這兩個接口供用戶使用。

1. CommandLineRunner

1.1 聲明:

@FunctionalInterface
public interface CommandLineRunner {

  /**
   * Callback used to run the bean.
   * @param args incoming main method arguments
   * @throws Exception on error
   */
  void run(String... args) throws Exception;

}

1.2 使用:

package com.example.consoleapplication;

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

@Component
public class TestRunner implements CommandLineRunner {

  @Override
  public void run(String... args) {
    // Do something...
    for(String arg: args){
      System.out.println(arg);
    }
    System.out.print("test command runner");
  }
}

1.3 運(yùn)行結(jié)果

運(yùn)行: java -jar build/libs/consoleapplication-0.0.1-SNAPSHOT.jar -sdfsaf sdfas,

結(jié)果如下:

2019-03-16 17:31:56.544  INFO 18679 --- [           main] c.e.consoleapplication.DemoApplication   : No active profile set, falling back to default profiles: default
2019-03-16 17:31:57.195  INFO 18679 --- [           main] c.e.consoleapplication.DemoApplication   : Started DemoApplication in 16.172 seconds (JVM running for 16.65)
-sdfsaf
sdfas
test command runner%

2. ApplicationRunner

2.1 聲明

/**
 * Interface used to indicate that a bean should <em>run</em> when it is contained within
 * a {@link SpringApplication}. Multiple {@link ApplicationRunner} beans can be defined
 * within the same application context and can be ordered using the {@link Ordered}
 * interface or {@link Order @Order} annotation.
 *
 * @author Phillip Webb
 * @since 1.3.0
 * @see CommandLineRunner
 */
@FunctionalInterface
public interface ApplicationRunner {

  /**
   * Callback used to run the bean.
   * @param args incoming application arguments
   * @throws Exception on error
   */
  void run(ApplicationArguments args) throws Exception;

}

2.2 使用

ApplicationRunner 和 CommandLineRunner 的使用是有差別的:

  • CommandLineRunner 的使用,只是把參數(shù)根據(jù)空格分割。
  • ApplicationRunner 會根據(jù) 是否匹配 --key=value 來解析參數(shù),
    • 能匹配,則為 optional 參數(shù), 可用getOptionValues獲取參數(shù)值。
    • 不匹配則是 non optional 參數(shù)。
package com.example.consoleapplication;

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

@Component
public class TestApplicationRunner implements ApplicationRunner {

  @Override
  public void run(ApplicationArguments args) throws Exception {
    // Do something...
    System.out.println("option arg names" + args.getOptionNames());
    System.out.println("non option+" + args.getNonOptionArgs());
  }
}

2.3 運(yùn)行結(jié)果

運(yùn)行命令 java -jar build/libs/consoleapplication-0.0.1-SNAPSHOT.jar -non1 non2 --option=1, 結(jié)果為:

2019-03-16 18:08:08.528  INFO 19778 --- [           main] c.e.consoleapplication.DemoApplication   : No active profile set, falling back to default profiles: default
2019-03-16 18:08:09.166  INFO 19778 --- [           main] c.e.consoleapplication.DemoApplication   : Started DemoApplication in 16.059 seconds (JVM running for 16.56)
test
option arg names[option]
non option+[-non1, non2]-non1
non2
--option=1
test%

可以看到, optional 參數(shù)名有 option, non optional 參數(shù)有 -non1 和 non2

3. 小結(jié)

CommandLineRunner 和 ApplicationRunner 都能實(shí)現(xiàn)命令行應(yīng)用啟動時根據(jù)參數(shù)獲取我們需要的值,做特殊的邏輯。但兩者有所不同,推薦使用 ApplicationRunner 的 optional 參數(shù), 方便擴(kuò)展。

4. 參考文檔

https://docs.spring.io/spring-boot/docs/2.0.5.RELEASE/reference/htmlsingle/#boot-features-web-environment

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java方法簽名為何不包含返回值類型

    Java方法簽名為何不包含返回值類型

    這篇文章主要介紹了Java方法簽名為何不包含返回值類型,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-06-06
  • Spring Boot使用線程池創(chuàng)建多線程的完整示例

    Spring Boot使用線程池創(chuàng)建多線程的完整示例

    在 Spring Boot 2 中,可以使用 @Autowired 注入 線程池(ThreadPoolTaskExecutor 或 ExecutorService),從而管理線程的創(chuàng)建和執(zhí)行,以下是使用 @Autowired 方式注入線程池的完整示例,感興趣的朋友一起看看吧
    2025-03-03
  • java中InputStream轉(zhuǎn)為MultipartFile的解決方案

    java中InputStream轉(zhuǎn)為MultipartFile的解決方案

    這篇文章主要介紹了java中InputStream轉(zhuǎn)為MultipartFile的解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2025-03-03
  • SpringMVC解析JSON請求數(shù)據(jù)問題解析

    SpringMVC解析JSON請求數(shù)據(jù)問題解析

    這篇文章主要介紹了SpringMVC解析JSON請求數(shù)據(jù)問題解析,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-04-04
  • SpringBoot調(diào)用service層的三種方法

    SpringBoot調(diào)用service層的三種方法

    在Spring?Boot中,我們可以通過注入Service層對象來調(diào)用Service層的方法,Service層是業(yè)務(wù)邏輯的處理層,它通常包含了對數(shù)據(jù)的增刪改查操作,本文給大家介紹了SpringBoot調(diào)用service層的三種方法,需要的朋友可以參考下
    2024-05-05
  • 將本地JAR文件手動添加到Maven本地倉庫的實(shí)現(xiàn)過程

    將本地JAR文件手動添加到Maven本地倉庫的實(shí)現(xiàn)過程

    在Java開發(fā)中,使用Maven作為項(xiàng)目管理工具已經(jīng)成為了主流的選擇,Maven提供了強(qiáng)大的依賴管理功能,可以輕松地下載和管理項(xiàng)目所需的庫和工具,在某些情況下,你可能會需要將本地下載的JAR文件手動添加到Maven的本地倉庫中,這篇博客將詳細(xì)介紹如何實(shí)現(xiàn)這一過程
    2024-10-10
  • SpringBoot 整合 RabbitMQ 的使用方式(代碼示例)

    SpringBoot 整合 RabbitMQ 的使用方式(代碼示例)

    本文詳細(xì)介紹了使用RabbitTemplate進(jìn)行消息傳遞的幾種模式,包括點(diǎn)對點(diǎn)通信、發(fā)布/訂閱模式、工作隊(duì)列模式、路由模式和主題模式,每種模式都通過代碼示例展示了生產(chǎn)者和消費(fèi)者的實(shí)現(xiàn),幫助開發(fā)者理解和運(yùn)用RabbitMQ進(jìn)行高效的消息處理
    2024-10-10
  • springboot @ConditionalOnMissingBean注解的作用詳解

    springboot @ConditionalOnMissingBean注解的作用詳解

    這篇文章主要介紹了springboot @ConditionalOnMissingBean注解的作用詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-08-08
  • Java多線程之異步Future機(jī)制的原理和實(shí)現(xiàn)

    Java多線程之異步Future機(jī)制的原理和實(shí)現(xiàn)

    這篇文章主要為大家詳細(xì)介紹了Java多線程之異步Future機(jī)制的原理和實(shí)現(xiàn),感興趣的小伙伴們可以參考一下
    2016-08-08
  • Java中的static和final關(guān)鍵字的使用詳解

    Java中的static和final關(guān)鍵字的使用詳解

    這篇文章主要介紹了Java中的static和final關(guān)鍵字的使用詳解,  當(dāng)方法名前有static,即為static方法,可以方便我們無需創(chuàng)建對象也可以調(diào)用此方法,靜態(tài)方法比較拉,只可以訪問 靜態(tài)的 屬性/變量/方法,無法訪問非靜態(tài)的這些屬性/變量/方法,需要的朋友可以參考下
    2024-01-01

最新評論

巴青县| 桐梓县| 循化| 星子县| 天祝| 古蔺县| 武宣县| 老河口市| 巴青县| 营口市| 集贤县| 双城市| 景谷| 新津县| 吉林省| 临夏县| 延安市| 清流县| 正宁县| 会泽县| 汉沽区| 成武县| 盐山县| 宝山区| 万盛区| 雅安市| 神池县| 上高县| 福州市| 布尔津县| 高青县| 鄂州市| 绵阳市| 湘潭市| 津市市| 交城县| 慈利县| 苏尼特左旗| 香港 | 青冈县| 高要市|