Command line arguments in spring boot

Posted By : Rozi Ali | 04-Dec-2020

Introduction

 

Command-line arguments are used to pass data during run-time. In spring-boot, any command-line arguments by default add to spring Environment and if same property already presents in it then that will be replaced by the one that is passed as a command-line argument. In this blog, we'll show how to use command-line arguments in spring boot.

 

Passing Command-line Arguments

 

In spring boot 2.x, we can pass the command line arguments separated by space and prefixed by "--". Syntax for command line argument is mvn spring-boot:run -Dspring-boot.run.arguments="--id=001 --name=John"

 

Using command line arguments, we can also set or update environment variables. Command line arguments have more privilage for environment variables over other property sources and if any command line argument have name similar to any property in application.properties then the value will be overwritten with the command line argument's value. For instance, if you have a port number saved in application.properties

 

server.port=8080

 

And now, you want to change it during runtime, you can use the command:

 

mvn spring-boot:run -Dspring-boot.run.arguments=--server.port=9090

 

Reading Command-line Argument

 

Command line arguments stores in an array, when spring application starts, it main method executes which gets these arguments in a parameter. To read the arguments, all we have to do is reading the array.

 

@SpringBootApplication
public class CommandLineArgumentsReadExample {
 public static void main(String[] args) {
      for(String arg:args) {
            System.out.println(arg);
      }
SpringApplication.run(CommandLineArgumentsExampleApplication.class, args);
 }
}

 

Note: Arguments pass by command line stores in array in the same sequence

 

I hope you find this blog useful :)

 

We, at Oodles Technologies, provide full-scale SaaS app development services to build scalable, responsive, and quality-driven web and mobile applications. Contact us for technical assistance or drop us a line at [email protected] for more detail. 

About Author

Author Image
Rozi Ali

Rozi Ali is an accomplished software developer with extensive experience in the field of JAVA. She possesses a solid grasp of programming languages such as Java/Spring-boot, Python, and Typescript/Nodejs/GraphQL. Rozi has a strong background in Object-oriented programming (OOP) and is skilled in working with both relational databases like MySql, PostgreSQL and non-relational databases like MongoDb. She is proficient in REST APIs, Microservices, and code deployment, along with the development tools such as Jira, Git, and Bash. Additionally, Rozi has experience working with Cloud providers such as AWS and Azure. She has contributed significantly to a number of projects, including Konfer, VNS, Influsoft, VN Platform, QuickDialog, and Oodles-Dashboard.

Request for Proposal

Name is required

Comment is required

Sending message..