亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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 運行結(jié)果

運行: 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 運行結(jié)果

運行命令 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 都能實現(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編寫局域網(wǎng)多人聊天室

    基于java編寫局域網(wǎng)多人聊天室

    這篇文章主要為大家詳細(xì)介紹了基于java編寫局域網(wǎng)多人聊天室的相關(guān)資料,使用socket基于java編寫一個局域網(wǎng)聊天室,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-09-09
  • MyBatis攔截器動態(tài)替換表名的方法詳解

    MyBatis攔截器動態(tài)替換表名的方法詳解

    因為我們持久層框架更多地使用MyBatis,那我們就借助于MyBatis的攔截器來完成我們的功能,這篇文章主要給大家介紹了關(guān)于MyBatis攔截器動態(tài)替換表名的相關(guān)資料,需要的朋友可以參考下
    2022-04-04
  • 使用SpringAOP實現(xiàn)公共字段填充功能

    使用SpringAOP實現(xiàn)公共字段填充功能

    在新增員工或者新增菜品分類時需要設(shè)置創(chuàng)建時間、創(chuàng)建人、修改時間、修改人等字段,在編輯員工或者編輯菜品分類時需要設(shè)置修改時間、修改人等字段,這些字段屬于公共字段,本文將給大家介紹使用SpringAOP實現(xiàn)公共字段填充功能,需要的朋友可以參考下
    2024-08-08
  • SpringBoot自定義錯誤處理邏輯詳解

    SpringBoot自定義錯誤處理邏輯詳解

    這篇文章主要介紹了SpringBoot自定義錯誤處理邏輯,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧
    2022-10-10
  • Spring Boot 與 Kotlin 上傳文件的示例代碼

    Spring Boot 與 Kotlin 上傳文件的示例代碼

    這篇文章主要介紹了Spring Boot 與 Kotlin 上傳文件的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-01-01
  • 基于SpringBoot創(chuàng)建Web頁面并熱更新的操作步驟

    基于SpringBoot創(chuàng)建Web頁面并熱更新的操作步驟

    SpringBoot是一個用于快速開發(fā)單個微服務(wù)的框架,它基于 Spring 框架,簡化了Spring應(yīng)用的初始化過程和開發(fā)流程,本文給大家介紹了如何基于SpringBoot創(chuàng)建Web頁面并熱更新,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下
    2023-11-11
  • java?guava主要功能介紹及使用心得總結(jié)

    java?guava主要功能介紹及使用心得總結(jié)

    這篇文章主要為大家介紹了java?guava主要功能介紹及使用心得總結(jié),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-04-04
  • JAVA正則表達(dá)式提取key-value類型字符值代碼實例

    JAVA正則表達(dá)式提取key-value類型字符值代碼實例

    這篇文章主要給大家介紹了關(guān)于JAVA正則表達(dá)式提取key-value類型字符值的相關(guān)資料,文中通過實例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2022-10-10
  • Springboot集成Ehcache3實現(xiàn)本地緩存的配置方法

    Springboot集成Ehcache3實現(xiàn)本地緩存的配置方法

    EhCache是一個純Java的進(jìn)程內(nèi)緩存框架,是 Hibernate 中默認(rèn)的 CacheProvider,同Redis一樣,EhCache 不是純內(nèi)存緩存,它支持基于內(nèi)存和磁盤的二級緩存,本文介紹Springboot集成Ehcache3實現(xiàn)本地緩存的配置方法,感興趣的朋友一起看看吧
    2024-04-04
  • java模板引擎Thymeleaf和前端vue的區(qū)別及說明

    java模板引擎Thymeleaf和前端vue的區(qū)別及說明

    這篇文章主要介紹了java模板引擎Thymeleaf和前端vue的區(qū)別及說明,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-11-11

最新評論