Spring Boot CommandLineRunner Example

1. Introduction

Spring Boot simplifies the execution of code at the startup of an application through the CommandLineRunner interface. This interface provides a single run method, which is called with command-line arguments just before Spring Boot finishes its startup. This feature is particularly useful for running initialization logic, such as pre-loading data into a database or setting up resources. This blog post will demonstrate how to use the CommandLineRunner in a Spring Boot application.

Key Points:

1. CommandLineRunner is an interface used to indicate that a bean should run when it is contained within a SpringApplication.

2. Beans implementing CommandLineRunner are automatically picked up and executed by Spring Boot.

3. The run method receives the command-line arguments passed to the application.

4. Multiple CommandLineRunner beans can be defined within the same application, and they can be ordered using the @Order annotation.

5. It is a simple and effective way to perform actions on application startup.

2. Implementation Steps

1. Create a Spring Boot application with the necessary dependencies.

2. Implement the CommandLineRunner interface in a bean and define the run method.

3. Use dependency injection if your startup logic requires other beans from the application context.

4. Optionally, use the @Order annotation if you need to define a specific order for your runners.

5. Run the Spring Boot application and observe the actions performed by the CommandLineRunner.

3. Implementation Example

// Step 1: Create a class that implements CommandLineRunner
@Component
public class StartupRunner implements CommandLineRunner {

    // Step 2: Implement the run method
    @Override
    public void run(String... args) throws Exception {
        // Step 3: Write the logic that should be executed on startup
        System.out.println("The CommandLineRunner is executing!");
        for (String arg : args) {
            System.out.println("Arg: " + arg);
        }
    }
}

// Step 4: Create the main Spring Boot application class
@SpringBootApplication
public class CommandLineRunnerApplication {

    public static void main(String[] args) {
        // Step 5: Pass command-line arguments to SpringApplication.run()
        SpringApplication.run(CommandLineRunnerApplication.class, args);
    }
}

// Upon running the application, the messages from the CommandLineRunner will be printed to the console.

Output:

// The output in the console will be:
The CommandLineRunner is executing!
Arg: --spring.output.ansi.enabled=always
// ... followed by any other command-line arguments passed to the application.

Explanation:

1. StartupRunner: This bean implements CommandLineRunner, which means it contains startup logic to be executed.

2. @Component: This annotation marks StartupRunner as a component, which makes it eligible for component scanning by Spring.

3. @Override: This indicates that the run method is overriding the method in the CommandLineRunner interface.

4. String... args: The run method's parameter that will receive the command-line arguments.

5. CommandLineRunnerApplication: The main class is annotated with @SpringBootApplication, which serves as the bootstrap for the application.

6. SpringApplication.run(): This static method starts the entire Spring framework. The CommandLineRunnerApplication.class argument tells Spring where to start component scanning, and args are the command-line arguments passed through.


Comments