In Spring Boot, command-line arguments offer a flexible way to configure applications at runtime, allowing you to override default settings or pass custom parameters without altering the codebase. Here's a structured overview:
1. Passing Command-Line Arguments
You can supply command-line arguments to a Spring Boot application in various ways, depending on how you run the application:
- Executable JAR: When running the packaged JAR file, append arguments after the JAR name:
java -jar myapp.jar --server.port=8081 --custom.argument=value
-
Maven Plugin: If using the Spring Boot Maven plugin, pass arguments using the
-Dspring-boot.run.arguments
property:
mvn spring-boot:run -Dspring-boot.run.arguments="--server.port=8081,--custom.argument=value"
-
Gradle Plugin: Configure the
bootRun
task in yourbuild.gradle
to accept arguments:
bootRun {
if (project.hasProperty('args')) {
args project.args.split(',')
}
}
Then run:
./gradlew bootRun --args="--server.port=8081,--custom.argument=value"
These methods allow you to override default configurations or provide custom parameters at runtime. (baeldung.com)
2. Accessing Command-Line Arguments in Application Code
Spring Boot provides interfaces to access command-line arguments within your application:
-
Implementing
CommandLineRunner
: This interface allows you to execute code with access to the command-line arguments after the application context is loaded:
@SpringBootApplication
public class MyApp implements CommandLineRunner {
public static void main(String[] args) {
SpringApplication.run(MyApp.class, args);
}
@Override
public void run(String... args) {
for (String arg : args) {
System.out.println(arg);
}
}
}
-
Implementing
ApplicationRunner
: Similar toCommandLineRunner
but provides theApplicationArguments
interface for more structured access:
@SpringBootApplication
public class MyApp implements ApplicationRunner {
public static void main(String[] args) {
SpringApplication.run(MyApp.class, args);
}
@Override
public void run(ApplicationArguments args) {
if (args.containsOption("custom.argument")) {
System.out.println("Custom Argument: " + args.getOptionValues("custom.argument"));
}
}
}
These approaches enable you to handle both option arguments (e.g., --custom.argument=value
) and non-option arguments. (baeldung.com)
3. Overriding Application Properties
Command-line arguments can override properties defined in application.properties
or application.yml
. For instance, to change the server port:
- Command-Line:
java -jar myapp.jar --server.port=8081
- Maven:
mvn spring-boot:run -Dspring-boot.run.arguments="--server.port=8081"
In the application's property hierarchy, command-line arguments have the highest precedence, ensuring they override other configurations. (docs.spring.io)
4. Disabling Command-Line Property Conversion
By default, Spring Boot converts command-line arguments to properties and adds them to the Spring Environment
. To disable this behavior:
@SpringBootApplication
public class MyApp {
public static void main(String[] args) {
SpringApplication app = new SpringApplication(MyApp.class);
app.setAddCommandLineProperties(false);
app.run(args);
}
}
This prevents command-line arguments from being added to the Environment
, which can be useful in certain scenarios. (docs.spring.io)
For a visual demonstration, you might find this video helpful:
Top comments (0)