Member-only story
What is CommandLineRunner in Spring Boot?
Learn what CommandLineRunner is in Spring Boot, how it works, and how to use it with real-world examples. Ideal for initialization logic after application startup.
Ever wanted to run some custom logic right after your Spring Boot application starts?
Maybe initialize data, call an API, or load a file?
That’s where CommandLineRunner
comes in.
Spring Boot provides a simple interface — CommandLineRunner
— to execute code after the application context is fully loaded.
Let’s explore how it works and when to use it.
🔍 What is CommandLineRunner?
CommandLineRunner
is a functional interface in Spring Boot.
It is used to execute code after the Spring Boot application has started and all beans are initialized.
@FunctionalInterface
public interface CommandLineRunner {
void run(String... args) throws Exception;
}
It gets called after the application context is loaded and right before the application becomes fully operational.
🔧 How to Use CommandLineRunner
✅ Example:
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
@Component
public class AppStartupRunner implements CommandLineRunner {
@Override
public void run(String... args) throws…