Command Line Properties in Spring Boot

Learn via video courses
Topics Covered

Overview

Command line arguments are arguments given to a program from the command line. Every programming language accepts command-line arguments in some way. Command line arguments are useful as user input or customizing application behaviour.

Introduction to Command Line Arguments

Any self-executable java application can accept command-line arguments. Command line argument can be user input or signal to apply different behaviour based on the type of argument.

, For example,, a simple java sum program can accept two input numbers as command line arguments from the user.

Running the above program with the command line argument java -jar test.jar 1 3 should print sum is 13 on the console.

Passing Command Line Argument

Spring boot also has an entry point from the main method and accepts command line argument(s). Spring boot also accepts spring boot-specific arguments to customize the application behaviour.

The above code accepts one command line argument and prints it on the console.

We will cover three ways to supply command line arguments to spring boot.

  • Supply with the executable jar.
  • Supply argument with maven.
  • Supply argument with Gradle.

Passing Single Argument

  • Supply With Executable Jar: - When executable jars are created, we can provide a command line argument like any other java program.
  • Supply With Maven: - When using maven to run the spring boot application command line argument can be supplied using spring-boot.run.arguments
  • Supply with Gradle: - We first need to configure the bootRun task for Gradle to accept the command line argument.

Then command line argument can be supplied using the command.

All the above mechanisms should print the message argument supplied 5 on the console.

Passing Multiple Arguments

We can also supply more than one argument.

  • Supply With Executable Jar - To supply multiple arguments, separate them by a single space.
  • Supply With Maven - From maven, it is similar; separate argument by space.
  • Supply With Gradle - Gradle does not enforce any specific pattern for supplying multiple arguments. It's configured with the bootRun task. We have configured the bootRun task to accept arguments separated by space. Therefore to consume multiple argument commands will be:

Accessing Command-Line Arguments

Spring boot provides interfaces CommandLineRunner to access command line property into the application. CommandLineRunner is a functional interface that contains exactly one method run.

  • CommandLineRunner#run has a signature like below

The supplied arguments can be accessed from the run method.

Overriding System Properties

Sprint boot takes the command line one step further. It also allows you to customize spring boot application behaviour by accepting spring boot-specific properties. For example, the server port can be changed using the server.port properties from the command line.
Command line properties always supersede the same property supplied elsewhere.

Let's see that in action. We are changing the application port to 9090 using all the mechanisms.

  • Executable JAR - Spring-specific properties can be specified using the -- pattern.

The above command will start tomcat on port 9090 and turn off the spring boot banner.

  • Using Maven - Spring-specific argument can be supplied using the pattern mvn spring-boot:run -Dspring-boot.run.arguments="--arg1=value --arg2=value"

The same setting as above can be activated using the below maven command.

  • Using Gradle - Using Gradle same setting can be activated using the below command

Conclusion

  • Command line argument can be supplied to spring boot application from the runnable jar, maven, or Gradle.
  • Each mechanism has its way of supplying an argument.
  • Spring boot also accepts spring-specific properties from the command line arguments, which helps customize the application behaviour based on the environment at runtime.