如何将程序参数传递给Spring Boot命令行应用程序?

huangapple go评论96阅读模式
英文:

How to pass programs arguments to spring boot command line application?

问题

Java程序通常在eclipse上有一个选项来传递参数:

我正在尝试在springboot和maven中做同样的事情,但没有选项可以这样做。

如何将参数传递给主类?

public static void main(String[] args) {
    // 禁用横幅,不想看到Spring标志
    SpringApplication app = new SpringApplication(EnvioTelesapApplication.class);
    app.setBannerMode(Banner.Mode.OFF);

    app.run(args);
}

编辑:

使用 -Dspring-boot.run.arguments="T111111,SS" 将其视为一个参数。

英文:

Java program usually have an option on eclipse to pass the arguments:
如何将程序参数传递给Spring Boot命令行应用程序?

I'm trying to do the same with springboot and maven, but there is no option to do that
如何将程序参数传递给Spring Boot命令行应用程序?

How can I pass the arguments to the main class?

public static void main(String[] args) {
	// disabled banner, don't want to see the spring logo
	SpringApplication app = new SpringApplication(EnvioTelesapApplication.class);
	app.setBannerMode(Banner.Mode.OFF);

	app.run(args);
}

EDIT :

Using -Dspring-boot.run.arguments="T111111,SS" is taking all as one argument

答案1

得分: 3

使用Spring Boot时,您可能应该充分利用application.properties文件,并根据需要进行覆盖(包括通过命令行)。但是,由于您是将Spring应用程序作为Maven构建而不是Java应用程序运行的,因此在运行时可能看不到“arguments”选项卡。在这种情况下,您需要将参数作为Maven目标的一部分传递:

  • Spring Boot 1.x:spring-boot:run -Drun.arguments=--spring.main.banner-mode=off,--customArgument=custom
  • Spring Boot 2.x:spring-boot:run -Dspring-boot.run.arguments=--spring.main.banner-mode=off,--customArgument=custom

详细了解Spring中的配置工作方式:https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-external-config

编辑

一旦属性被定义(在application-properties文件、命令行参数、环境变量等中),可以通过@Value("${foo}")来检索属性,其中foo是属性名。

此外,如果您有兴趣将命令行参数传递给通过jar文件运行的Spring Boot应用程序,语法将是:java -jar myjar.jar --argOne=value --argTwo=value,正如Spring Boot文档中所详细说明的那样。

英文:

When using spring boot you probably should be taking advantages of application.properties file and overriding them as needed (including via command line).
Still, you don't see the 'arguments' tabs as you're running your spring application as a maven build rather than a Java application. In this case, you'd need to pass the arguments as part of the maven goal itself:

  • Spring Boot 1.x: spring-boot:run -Drun.arguments=--spring.main.banner-mode=off,--customArgument=custom
  • Spring Boot 2.x: spring-boot:run -Dspring-boot.run.arguments=--spring.main.banner-mode=off,--customArgument=custom

Check how the configuration works in spring in more details: https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-external-config

Edit:

Once the properties are defined (in a application-properties file, command line arguments, environment variable, etc.) it can be retrieved by means of @Value("${foo}") where foo is the property name.

Also, if you are interested in passing command line arguments to a Spring Boot application running via a jar file, the syntax would be: java -jar myjar.jar --argOne=value --argTwo=value as well documented in
Spring Boot documentation

huangapple
  • 本文由 发表于 2020年8月25日 16:23:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/63574837.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定