🏠Spring BootAccessing Command-line Arguments in Spring Boot

Accessing Command-line Arguments in Spring Boot

Let’s learn how to read command-line arguments of a Spring Boot Application.

Like any other java program, Spring Boot can take program parameters of application parameters on startup. But spring boot goes extra-mile to argument parsing. Lets get into the details.

CommandLineRunner and ApplicationRunner

These two interfaces are similar in nature and help you access the command line arguments.

CommandLineRunner to read arguments.

The CommandLineRunner interface in Spring Boot lets you access all the raw command-line args. We have a detailed article about command-line runners in spring boot if you would like to read. Basically, you can write a Bean or Component of type CommandLineRunner to access the arguments. For example, lets write our runner as seen here.

@Component public class StartupPrintRunner implements CommandLineRunner { @Override public void run(String... args) throws Exception { System.out.println("args = " + Arrays.deepToString(args)); } }
Code language: Java (java)

And if we run the application with few arguments, you can see the application printing them.

java -jar spring-boot-example.jar first-argument second-argument third-argument
Code language: Bash (bash)

Further on this, lets see some of the disadvantages of CommandLineRunner.

  1. You only get the arguments as a list of Strings.
  2. There is no argument parsing. For example, if you send --some-option=some-value, then they will be taken as single argument (["--some-option=some-value"]).
  3. You cannot inject these arguments into other components directly.

ApplicationRunner for Parsing Command-line arguments

By default, Spring boot provides a bean of type ApplicationArguments. This bean contains all parsed and non-parsed commandline arguments. Importantly, you can autowire this anywhere you want.

The important feature of the ApplicationArguments is that they could parse arguments as key value pair. For example, you can pass –some-argument=some-value. This parameter will then be parsed into a key value pair of { “some-argument”:”some-value” }.

@Component public class SomeComponent { private final ApplicationArguments applicationArguments; public SomeComponent(ApplicationArguments applicationArguments) { this.applicationArguments = applicationArguments; } //do something with applicationArguments in methods }
Code language: Java (java)

But if you would like to get a similar behaviour as CommandLineRunner, then you should use the ApplicationRunner interface. And here is how.

@Component public class StartupApplicationRunner implements ApplicationRunner { @Override public void run(ApplicationArguments args) throws Exception { System.out.println("args.getOptionNames() = " + args.getOptionNames()); System.out.println("args.containsOption(\"first-argument\") = " + args.containsOption("first-argument")); System.out.println("args.getOptionValues(\"first-argument\") = " + args.getOptionValues("first-argument")); System.out.println("args.getNonOptionArgs() = " + args.getNonOptionArgs()); args.getNonOptionArgs(); } }
Code language: Java (java)

Note that, we have used a wide range of methods from ApplicationArguments interface.

  1. args.getOptionNames() – Returns a list of parsed parameter keys.
  2. args.containsOption() – Checks and returns a boolean based on the presense of given option.
  3. args.getOptionValues() – returns all matching values as an array for the given option(Yes, you can add an option more than once)
  4. args.getNonOptionArgs() – returns all orphan arguments that didn’t follow the –option=value format.

If you are interested, then you can run the application using the following commands and see the results yourself.

mvn clean install java - jar spring-boot-command-line-args-0.0.1-SNAPSHOT.jar --first-argument=first-value --second-argument=second-value third-argument
Code language: Bash (bash)

Spring Boot PropertySource for Application Arguments

Spring Boot also registers a CommandLinePropertySource in the Spring context. This property source allows us to directly inject parsed parameters into components.

@Component public class SomeComponent { @Value("${second-argument}") private String secondArgument; //more autowiring }
Code language: Java (java)

This approach is equivilant to how application.properties behave. This means, you can overrride application properties using Command-Line arguments.

Summary

To sum it up, we learned various ways to access application arguments in spring boot. You can find all these examples in our GitHub repository.

Related

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *