Spring Boot CommandLineRunner Interface Example


Spring Boot’s CommandLineRunner interface, allows developers to execute code when the Spring Boot application starts. In this blog, we’ll explore the CommandLineRunner interface, and its purpose, and provide examples of its usage.

Understanding the CommandLineRunner Interface

The CommandLineRunner interface is part of the Spring Boot framework. It belongs to the org.springframework.boot package and is designed to execute code at the startup of a Spring Boot application. It is a functional interface, which means it has a single abstract method, void run(String... args). This method is invoked when the Spring application context is initialized.

The run method receives a variable number of command-line arguments as an array of strings. This allows you to pass arguments to your application at startup, which can be useful for configuration and customization.

Let’s explore some common usages of the CommandLineRunner interface.

1. Basic Usage

In this example, we’ll create a simple Spring Boot application that prints a message at startup. First, create a Spring Boot application with the Spring Initializer or your favorite IDE.

SpringBootApp.java
package org.websparrow;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringBootApp implements CommandLineRunner {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootApp.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        System.out.println("Hello from CommandLineRunner!");
    }
}

When you run the application, it will print “Hello from CommandLineRunner!” during startup.

Similar Posts:

  1. Spring Boot Cookies Example
  2. Spring Boot- Disable Spring banner at startup
  3. Spring Boot + MongoDB CRUD Example

2. Using Command-Line Arguments

The CommandLineRunner interface allows you to access command-line arguments passed to your application. Let’s modify our example to use these arguments:

SpringBootApp.java
package org.websparrow;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringBootApp implements CommandLineRunner {
    public static void main(String[] args) {
        SpringApplication.run(SpringBootApp.class, "arg1", "arg2");
    }

    @Override
    public void run(String... args) throws Exception {
        System.out.println("Command-Line Arguments:");
        for (String arg : args) {
            System.out.println(arg);
        }
    }
}

Now, when you run the application, it will print the command-line arguments passed to it.

3. Executing Custom Logic

You can use the CommandLineRunner to execute custom logic or perform tasks at application startup. For instance, you can load configuration settings, initialize database connections, or perform any other setup needed for your application.

SpringBootApp.java
package org.websparrow;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringBootApp implements CommandLineRunner {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootApp.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        // Custom initialization logic
        System.out.println("Executing custom initialization logic...");
    }
}

This demonstrates how you can execute custom logic during application startup.

Summary

The CommandLineRunner interface is a valuable tool in Spring Boot for executing code when your application starts. Whether you want to print messages, process command-line arguments, or perform complex initialization tasks, this interface can be a powerful addition to your Spring Boot applications. In summary, the CommandLineRunner interface simplifies the process of running code at application startup, making Spring Boot even more flexible and powerful for developers.

References

  1. Interface CommandLineRunner- SpringDoc
  2. Spring Boot application.properties vs. application.yml

Similar Posts

About the Author

Atul Rai
I love sharing my experiments and ideas with everyone by writing articles on the latest technological trends. Read all published posts by Atul Rai.