What is CommandLineRunner in SpringBoot?

CommandLineRunner is a Spring Boot interface that allows developers to execute custom code immediately after the application has started and the Spring context has been fully initialized. It is commonly used for startup tasks such as loading initial data, validating configurations, creating default records, or performing initialization logic.

Key Points: • CommandLineRunner executes automatically after the Spring Boot application starts. • It is useful for performing startup and initialization tasks. • Command-line arguments are received as a String array. • Multiple CommandLineRunner implementations can be executed in a specific order using @Order. • It runs only once during application startup.

Why Do We Need CommandLineRunner?

Some applications need to perform actions immediately after startup.

Examples:

• Insert default data into a database • Initialize cache • Validate application configuration • Check external service connectivity • Execute startup scripts

CommandLineRunner provides a simple way to execute such logic automatically.

How Does It Work?

Application Starts | Spring Context Loaded | Beans Created | CommandLineRunner Executes | Application Ready

The run() method is called automatically after the application startup process completes.

Code Example:

import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
public class StartupRunner
        implements CommandLineRunner {

    @Override
    public void run(String... args)
            throws Exception {

        System.out.println(
                "Application Started Successfully");
    }
}

Output:

Application Started Successfully

In this example:

• Spring creates the bean. • The run() method executes automatically after startup.

Accessing Command-Line Arguments

CommandLineRunner receives startup arguments through a String array.

Code Example:

@Component
public class StartupRunner
        implements CommandLineRunner {

    @Override
    public void run(String... args) {

        for (String arg : args) {

            System.out.println(arg);
        }
    }
}

Application Start:

java -jar app.jar prod test

Output:

prod
test

Common Use Cases

• Database initialization • Loading master data • Startup validation • Cache warming • Logging startup information • Executing startup jobs

Example: Suppose an e-commerce application needs product categories loaded into memory.

At startup:

• CommandLineRunner fetches categories. • Stores them in cache. • Makes them available for incoming requests.

This improves application performance.

CommandLineRunner vs ApplicationRunner

Both execute after application startup.

CommandLineRunner

Method:

public void run( String... args)

Features:

• Simple argument handling • Receives raw command-line arguments

ApplicationRunner

Method:

public void run( ApplicationArguments args)

Features:

• Structured argument access • Supports named parameters

Example:

Application Start:

java -jar app.jar --env=prod

ApplicationRunner can easily access:

env=prod

using ApplicationArguments.

CommandLineRunner only receives:

--env=prod

as a String value.

Executing Multiple Runners

Execution order can be controlled using @Order.

Code Example:

@Component

@Order(1)
public class FirstRunner
        implements CommandLineRunner {

}

@Component

@Order(2)
public class SecondRunner
        implements CommandLineRunner {

}

Execution Order:

FirstRunner

SecondRunner

Benefits of CommandLineRunner

• Simple startup execution • Easy initialization logic • Automatic execution after startup • Useful for setup tasks • Supports command-line arguments

Real-World Example

In a banking application:

After startup:

• Load currency exchange rates. • Verify database connectivity. • Initialize caches. • Validate external payment services.

CommandLineRunner can perform these tasks automatically before the application begins serving requests.

Interview Tip: A concise interview answer is:

"CommandLineRunner is a Spring Boot interface used to execute custom code immediately after the application starts and the Spring context is fully initialized. It is commonly used for startup tasks such as data initialization, cache loading, and configuration validation. The run() method receives command-line arguments as a String array and executes automatically during application startup."