What is the purpose of CommandLineRunner in Spring Boot?
In Spring Boot, the `CommandLineRunner` interface is a functional interface that provides a simple way to execute specific code once the Spring application context has loaded and is ready. It's particularly useful for performing tasks that need to run immediately after the application startup, before any web requests or other long-running processes begin.
Overview
The primary purpose of CommandLineRunner is to allow developers to execute arbitrary code with access to command-line arguments. Any bean that implements CommandLineRunner will have its run() method called by the SpringApplication. The run() method receives the application's command-line arguments as an array of strings.
This mechanism ensures that the code runs only after all Spring components are initialized, dependencies are injected, and the application context is fully operational. This makes it ideal for one-time initialization tasks or batch processing at startup.
Key Use Cases
- Data Initialization: Populating a database with default or test data when the application starts.
- Configuration Validation: Checking external resources or configuration settings after the application is up.
- Background Tasks: Starting some initial background processes or schedulers.
- Batch Processing: Executing a one-off batch job based on command-line arguments.
- Resource Loading: Loading application-specific resources or caches that are critical for the application's operation.
- Reporting: Generating initial reports or logs upon startup.
How it Works
To use CommandLineRunner, you create a class that implements the interface and mark it as a Spring component (e.g., with @Component). The run method takes a String... args parameter, which are the command-line arguments passed to the application. For example, if you run your JAR as java -jar myapp.jar arg1 arg2, then args will contain ["arg1", "arg2"].
Spring Boot supports multiple CommandLineRunner beans. If you have several, they will all be executed. The order of execution can be controlled by implementing the org.springframework.core.Ordered interface or by using the @Order annotation on the CommandLineRunner implementations.
Example
Here's a simple example of a CommandLineRunner that logs a message and prints the command-line arguments:
package com.example.demo;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import java.util.Arrays;
@Component
public class MyStartupRunner implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
System.out.println("Application started! Executing CommandLineRunner...");
if (args.length > 0) {
System.out.println("Command-line arguments received: " + Arrays.toString(args));
} else {
System.out.println("No command-line arguments provided.");
}
System.out.println("CommandLineRunner finished its task.");
}
}
If you build this Spring Boot application into a JAR and run it without arguments: java -jar your-app.jar, the output would include:
Application started! Executing CommandLineRunner...
No command-line arguments provided.
CommandLineRunner finished its task.
If you run it with arguments: java -jar your-app.jar --spring.profiles.active=dev myparam1 value2, the output would include:
Application started! Executing CommandLineRunner...
Command-line arguments received: [--spring.profiles.active=dev, myparam1, value2]
CommandLineRunner finished its task.