简体   繁体   中英

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

Java program usually have an option on eclipse to pass the arguments: 在此处输入图片说明

I'm trying to do the same with springboot and maven, but there is no option to do that在此处输入图片说明

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM