Spring Boot Passing Command Line Arguments Example
Command Line Arguments can be used to configure your application, pass data at runtime, or to overwrite default configuration options. By default SpringApplication will convert any command line option arguments (starting with ‘–‘, e.g. –server.port=9090) to a property and add it to the Spring Environment. Command line properties always take precedence over other property sources. In this tutorial we demonstrate how to pass, parse and access command line arguments using Spring Boot.
Maven Dependencies
We use Apache Maven to manage our projects dependencies. Add the following dependencies to your project.
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.memorynotfound.springboot</groupId> <artifactId>command-line</artifactId> <version>1.0.0-SNAPSHOT</version> <url>https://memorynotfound.com</url> <name>Spring Boot - ${project.artifactId}</name> <packaging>war</packaging> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.3.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <build> <finalName>command-line</finalName> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <mainClass>com.memorynotfound.springboot.Application</mainClass> </configuration> </plugin> </plugins> </build> </project>
Default Application.properties
The following application.properties is located in the src/main/resources folder, and holds a default property called person.name. We are going to overwrite this configuration property value using command line arguments.
person.name=Anonymous
Spring Boot – Passing Command Line Arguments Example
When implementing the ApplicationRunner interface, we need to overwrite the method public void run(ApplicationArguments args) throws Exception. Spring Boot will call this method during startup. Here, we can retrieve the command line arguments. In the following application we print all the incoming command line arguments to the console and do a simple operation to see if an option exists among the command line properties.
Note: By default SpringApplication will convert any command line option arguments (starting with ‘–’, e.g. –server.port=9000) to a property and add it to the Spring Environment. If you don’t want command line properties to be added to the Environment you can disable them using SpringApplication.setAddCommandLineProperties(false).
package com.memorynotfound.springboot; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import java.util.Arrays; @SpringBootApplication public class Application implements ApplicationRunner { private static final Logger logger = LoggerFactory.getLogger(Application.class); public static void main(String... args) throws Exception { SpringApplication.run(Application.class, args); } @Override public void run(ApplicationArguments args) throws Exception { logger.info("Application started with command-line arguments: {}", Arrays.toString(args.getSourceArgs())); logger.info("NonOptionArgs: {}", args.getNonOptionArgs()); logger.info("OptionNames: {}", args.getOptionNames()); for (String name : args.getOptionNames()){ logger.info("arg-" + name + "=" + args.getOptionValues(name)); } boolean containsOption = args.containsOption("person.name"); logger.info("Contains person.name: " + containsOption); } }
Passing Command Line Arguments
This controller demonstrates that’ll overwrite the default person.name property.
package com.memorynotfound.springboot; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @Value("${person.name}") private String name; @GetMapping public String hello(){ return "Hello, " + name + "!"; } }
Building executable with Maven
mvn clean package
Running spring boot app
We overwrite the default person.name configuration property with a new value. Arguments passed by the command line always have preference over the default configuration options.
java -jar command-line.jar \ this-is-a-non-option-arg \ --server.port=9090 \ --person.name=Memorynotfound.com
Console Output
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v1.5.3.RELEASE) 2017-05-29 13:13:03.906 INFO 9547 --- [ main] c.memorynotfound.springboot.Application : Application started with command-line arguments: [this-is-a-non-option-arg, --server.port=9090, --person.name=Memorynotfound.com] 2017-05-29 13:13:03.907 INFO 9547 --- [ main] c.memorynotfound.springboot.Application : NonOptionArgs: [this-is-a-non-option-arg] 2017-05-29 13:13:03.907 INFO 9547 --- [ main] c.memorynotfound.springboot.Application : OptionNames: [server.port, person.name] 2017-05-29 13:13:03.907 INFO 9547 --- [ main] c.memorynotfound.springboot.Application : arg-server.port=[9090] 2017-05-29 13:13:03.908 INFO 9547 --- [ main] c.memorynotfound.springboot.Application : arg-person.name=[Memorynotfound.com] 2017-05-29 13:13:03.908 INFO 9547 --- [ main] c.memorynotfound.springboot.Application : Contains person.name: true 2017-05-29 13:13:03.910 INFO 9547 --- [ main] c.memorynotfound.springboot.Application : Started Application in 2.716 seconds (JVM running for 3.2)
Console Output
When accessing http://localhost:9090/ we recieve the following output:
Hello, Memorynotfound.com!
Download
From:一号门
COMMENTS