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

SPRING BOOT啟動(dòng)命令參數(shù)及源碼詳析

 更新時(shí)間:2019年12月23日 08:36:38   作者:程序新視界  
這篇文章主要給大家介紹了關(guān)于SPRING BOOT啟動(dòng)命令參數(shù)及源碼分析的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用SPRING BOOT具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

前言

使用過(guò)Spring Boot,我們都知道通過(guò)java -jar可以快速啟動(dòng)Spring Boot項(xiàng)目。同時(shí),也可以通過(guò)在執(zhí)行jar -jar時(shí)傳遞參數(shù)來(lái)進(jìn)行配置。本文帶大家系統(tǒng)的了解一下Spring Boot命令行參數(shù)相關(guān)的功能及相關(guān)源碼分析。

命令行參數(shù)使用

啟動(dòng)Spring Boot項(xiàng)目時(shí),我們可以通過(guò)如下方式傳遞參數(shù):

java -jar xxx.jar --server.port=8081

默認(rèn)情況下Spring Boot使用8080端口,通過(guò)上述參數(shù)將其修改為8081端口,而且通過(guò)命令行傳遞的參數(shù)具有更高的優(yōu)先級(jí),會(huì)覆蓋同名的其他配置參數(shù)。

啟動(dòng)Spring Boot項(xiàng)目時(shí)傳遞參數(shù),有三種參數(shù)形式:

  • 選項(xiàng)參數(shù)
  • 非選項(xiàng)參數(shù)
  • 系統(tǒng)參數(shù)

選項(xiàng)參數(shù),上面的示例便是選項(xiàng)參數(shù)的使用方法,通過(guò)“–-server.port”來(lái)設(shè)置應(yīng)用程序的端口?;靖袷綖椤皑Cname=value”(“–”為連續(xù)兩個(gè)減號(hào))。其配置作用等價(jià)于在application.properties中配置的server.port=8081。

非選項(xiàng)參數(shù)的使用示例如下:

java -jar xxx.jar abc def 

上述示例中,“abc”和“def”便是非選項(xiàng)參數(shù)。

系統(tǒng)參數(shù),該參數(shù)會(huì)被設(shè)置到系統(tǒng)變量中,使用示例如下:

java -jar -Dserver.port=8081 xxx.jar

參數(shù)值的獲取

選項(xiàng)參數(shù)和非選項(xiàng)參數(shù)均可以通過(guò)ApplicationArguments接口獲取,具體獲取方法直接在使用參數(shù)的類(lèi)中注入該接口即可。

@RestController
public class ArgumentsController {
  @Resource
  private ApplicationArguments arguments;
}

通過(guò)ApplicationArguments接口提供的方法即可獲得對(duì)應(yīng)的參數(shù)。關(guān)于該接口后面會(huì)詳細(xì)講解。

另外,選項(xiàng)參數(shù),也可以直接通過(guò)@Value在類(lèi)中獲取,如下:

@RestController
public class ParamController {
  @Value("${server.port}")
  private String serverPort;
}

系統(tǒng)參數(shù)可以通過(guò)java.lang.System提供的方法獲取:

String systemServerPort = System.getProperty("server.port");

參數(shù)值的區(qū)別

關(guān)于參數(shù)值區(qū)別,重點(diǎn)看選項(xiàng)參數(shù)和系統(tǒng)參數(shù)。通過(guò)上面的示例我們已經(jīng)發(fā)現(xiàn)使用選項(xiàng)參數(shù)時(shí),參數(shù)在命令中是位于xxx.jar之后傳遞的,而系統(tǒng)參數(shù)是緊隨java -jar之后。

如果不按照該順序進(jìn)行執(zhí)行,比如使用如下方式使用選項(xiàng)參數(shù):

java -jar --server.port=8081 xxx.jar

則會(huì)拋出如下異常:

Unrecognized option: --server.port=8081
Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.

如果將系統(tǒng)參數(shù)放在jar包后面,問(wèn)題會(huì)更嚴(yán)重。會(huì)出現(xiàn)可以正常啟動(dòng),但參數(shù)無(wú)法生效。這也是為什么有時(shí)候明明傳遞了參數(shù)但是卻未生效,那很可能是因?yàn)榘褏?shù)的位置寫(xiě)錯(cuò)了。

這個(gè)錯(cuò)誤是最坑的,所以一定謹(jǐn)記:通過(guò)-D傳遞系統(tǒng)參數(shù)時(shí),務(wù)必放置在待執(zhí)行的jar包之前。

另外一個(gè)重要的不同是:通過(guò)@Value形式可以獲得系統(tǒng)參數(shù)和選項(xiàng)參數(shù),但通過(guò)System.getProperty方法只能獲得系統(tǒng)參數(shù)。

ApplicationArguments解析

上面提到了可以通過(guò)注入ApplicationArguments接口獲得相關(guān)參數(shù),下面看一下具體的使用示例:

@RestController
public class ArgumentsController {
  @Resource
  private ApplicationArguments arguments;
  @GetMapping("/args")
  public String getArgs() {
    System.out.println("# 非選項(xiàng)參數(shù)數(shù)量: " + arguments.getNonOptionArgs().size());
    System.out.println("# 選項(xiàng)參數(shù)數(shù)量: " + arguments.getOptionNames().size());
    System.out.println("# 非選項(xiàng)具體參數(shù):");
    arguments.getNonOptionArgs().forEach(System.out::println);
    System.out.println("# 選項(xiàng)參數(shù)具體參數(shù):");
    arguments.getOptionNames().forEach(optionName -> {
      System.out.println("--" + optionName + "=" + arguments.getOptionValues(optionName));
    });
    return "success";
  }
}

通過(guò)注入ApplicationArguments接口,然后在方法中調(diào)用該接口的方法即可獲得對(duì)應(yīng)的參數(shù)信息。

ApplicationArguments接口中封裝了啟動(dòng)時(shí)原始參數(shù)的數(shù)組、選項(xiàng)參數(shù)的列表、非選項(xiàng)參數(shù)的列表以及選項(xiàng)參數(shù)獲得和檢驗(yàn)。相關(guān)源碼如下:

public interface ApplicationArguments {
  /**
   * 原始參數(shù)數(shù)組(未經(jīng)過(guò)處理的參數(shù))
   */
  String[] getSourceArgs();
  /**
   * 選項(xiàng)參數(shù)名稱
   */
  Set<String> getOptionNames();
  /**
   * 根據(jù)名稱校驗(yàn)是否包含選項(xiàng)參數(shù)
   */
  boolean containsOption(String name);
  /**
   * 根據(jù)名稱獲得選項(xiàng)參數(shù)
   */
  List<String> getOptionValues(String name);
  /**
   * 獲取非選項(xiàng)參數(shù)列表
   */
  List<String> getNonOptionArgs();
}

命令行參數(shù)的解析

上面直接使用了ApplicationArguments的注入和方法,那么它的對(duì)象是何時(shí)被創(chuàng)建,何時(shí)被注入Spring容器的?

在執(zhí)行SpringApplication的run方法的過(guò)程中會(huì)獲得傳入的參數(shù),并封裝為ApplicationArguments對(duì)象。相關(guān)源代碼如下:

public ConfigurableApplicationContext run(String... args) {
  try {
    ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
    // ...
    prepareContext(context, environment, listeners, // ...
  } catch (Throwable ex) {
    // ...
  }
  return context;
}

在上述代碼中,通過(guò)創(chuàng)建一個(gè)它的實(shí)現(xiàn)類(lèi)DefaultApplicationArguments來(lái)完成命令行參數(shù)的解析。

DefaultApplicationArguments部分代碼如下:

public class DefaultApplicationArguments implements ApplicationArguments {
  private final Source source;
  private final String[] args;
  public DefaultApplicationArguments(String... args) {
    Assert.notNull(args, "Args must not be null");
    this.source = new Source(args);
    this.args = args;
  }
  // ...
  @Override
  public List<String> getOptionValues(String name) {
    List<String> values = this.source.getOptionValues(name);
    return (values != null) ? Collections.unmodifiableList(values) : null;
  }
  private static class Source extends SimpleCommandLinePropertySource {
    Source(String[] args) {
      super(args);
    }
    // ...
  }
}

通過(guò)構(gòu)造方法,將args賦值給成員變量args,其中接口ApplicationArguments中g(shù)etSourceArgs方法的實(shí)現(xiàn)在該類(lèi)中便是返回args值。

針對(duì)成員變量Source(內(nèi)部類(lèi))的設(shè)置,在創(chuàng)建Source對(duì)象時(shí)調(diào)用了其父類(lèi)SimpleCommandLinePropertySource的構(gòu)造方法:

public SimpleCommandLinePropertySource(String... args) {
  super(new SimpleCommandLineArgsParser().parse(args));
}

在該方法中創(chuàng)建了真正的解析器SimpleCommandLineArgsParser并調(diào)用其parse方法對(duì)參數(shù)進(jìn)行解析。

class SimpleCommandLineArgsParser {
  public CommandLineArgs parse(String... args) {
    CommandLineArgs commandLineArgs = new CommandLineArgs();
    for (String arg : args) {
      // --開(kāi)頭的選參數(shù)解析
      if (arg.startsWith("--")) {
        // 獲得key=value或key值
        String optionText = arg.substring(2, arg.length());
        String optionName;
        String optionValue = null;
        // 如果是key=value格式則進(jìn)行解析
        if (optionText.contains("=")) {
          optionName = optionText.substring(0, optionText.indexOf('='));
          optionValue = optionText.substring(optionText.indexOf('=')+1, optionText.length());
        } else {
          // 如果是僅有key(--foo)則獲取其值
          optionName = optionText;
        }
        // 如果optionName為空或者optionValue不為空但optionName為空則拋出異常
        if (optionName.isEmpty() || (optionValue != null && optionValue.isEmpty())) {
          throw new IllegalArgumentException("Invalid argument syntax: " + arg);
        }
        // 封裝入CommandLineArgs
        commandLineArgs.addOptionArg(optionName, optionValue);
      } else {
        commandLineArgs.addNonOptionArg(arg);
      }
    }
    return commandLineArgs;
  }
}

上述解析規(guī)則比較簡(jiǎn)單,就是根據(jù)“–”和“=”來(lái)區(qū)分和解析不同的參數(shù)類(lèi)型。

通過(guò)上面的方法創(chuàng)建了ApplicationArguments的實(shí)現(xiàn)類(lèi)的對(duì)象,但此刻還并未注入Spring容器,注入Spring容器是依舊是通過(guò)上述SpringApplication#run方法中調(diào)用的prepareContext方法來(lái)完成的。相關(guān)代碼如下:

private void prepareContext(ConfigurableApplicationContext context, ConfigurableEnvironment environment,
    SpringApplicationRunListeners listeners, ApplicationArguments applicationArguments, Banner printedBanner) {
  // ...
  ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
  // 通過(guò)beanFactory將ApplicationArguments的對(duì)象注入Spring容器
  beanFactory.registerSingleton("springApplicationArguments", applicationArguments);
  // ...
}

至此,關(guān)于Spring Boot中ApplicationArguments的相關(guān)源碼解析完成。

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。

相關(guān)文章

  • Java安全后端返回文件流方式

    Java安全后端返回文件流方式

    這篇文章主要介紹了Java安全后端返回文件流方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • Springboot Mybatis-Plus數(shù)據(jù)庫(kù)單元測(cè)試實(shí)戰(zhàn)(三種方式)

    Springboot Mybatis-Plus數(shù)據(jù)庫(kù)單元測(cè)試實(shí)戰(zhàn)(三種方式)

    這篇文章主要介紹了Springboot Mybatis-Plus數(shù)據(jù)庫(kù)單元測(cè)試實(shí)戰(zhàn)(三種方式),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • Spring?Boot獲取resources目錄下的文件三種方式詳解

    Spring?Boot獲取resources目錄下的文件三種方式詳解

    在Spring?Boot項(xiàng)目中,經(jīng)常需要獲取resources目錄下的文件,這些文件可以包括配置文件、模板文件、靜態(tài)資源等,這篇文章主要介紹了Spring?Boot獲取resources目錄下的文件的三種方式,需要的朋友可以參考下
    2023-06-06
  • 學(xué)習(xí)Java之如何正確地向上轉(zhuǎn)型與向下轉(zhuǎn)型

    學(xué)習(xí)Java之如何正確地向上轉(zhuǎn)型與向下轉(zhuǎn)型

    面向?qū)ο蟮牡谌齻€(gè)特征是多態(tài),實(shí)現(xiàn)多態(tài)有三個(gè)必要條件:繼承、方法重寫(xiě)和向上轉(zhuǎn)型,在學(xué)習(xí)多態(tài)之前,我們還要先學(xué)習(xí)Java的類(lèi)型轉(zhuǎn)換,本篇文章就來(lái)帶大家認(rèn)識(shí)什么是類(lèi)型轉(zhuǎn)換,看看類(lèi)型轉(zhuǎn)換都有哪幾種情況,以及如何避免類(lèi)型轉(zhuǎn)換時(shí)出現(xiàn)異常
    2023-05-05
  • Java 讀取類(lèi)路徑下的資源文件實(shí)現(xiàn)代碼

    Java 讀取類(lèi)路徑下的資源文件實(shí)現(xiàn)代碼

    這篇文章主要介紹了Java 讀取類(lèi)路徑下的資源文件實(shí)現(xiàn)代碼的相關(guān)資料,主要實(shí)現(xiàn)工具類(lèi)代碼ResourceLoadUtil.java的實(shí)例需要的朋友可以參考下
    2017-07-07
  • Java中Stringbuild,Date和Calendar類(lèi)的用法詳解

    Java中Stringbuild,Date和Calendar類(lèi)的用法詳解

    這篇文章主要為大家詳細(xì)介紹了Java中Stringbuild、Date和Calendar類(lèi)的用法,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起了解一下
    2023-04-04
  • Mybatis批量更新報(bào)錯(cuò)問(wèn)題

    Mybatis批量更新報(bào)錯(cuò)問(wèn)題

    這篇文章主要介紹了Mybatis批量更新報(bào)錯(cuò)的問(wèn)題及解決辦法,包括mybatis批量更新的兩種方式,需要的的朋友參考下
    2017-01-01
  • SpringBoot2.0整合SpringCloud Finchley @hystrixcommand注解找不到解決方案

    SpringBoot2.0整合SpringCloud Finchley @hystrixcommand注解找不到解決方案

    這篇文章主要介紹了SpringBoot2.0整合SpringCloud Finchley @hystrixcommand注解找不到解決方案,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-08-08
  • springboot?log4j2.xml如何讀取application.yml中屬性值

    springboot?log4j2.xml如何讀取application.yml中屬性值

    這篇文章主要介紹了springboot?log4j2.xml如何讀取application.yml中屬性值問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • Java實(shí)現(xiàn)快速并查集

    Java實(shí)現(xiàn)快速并查集

    這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)快速并查集,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-07-07

最新評(píng)論