Spring Boot Tutorial
Spring Boot - Software Setup and Configuration (STS/Eclipse/IntelliJ)
Prerequisite (Spring Core Concepts)
Spring Boot Core
Spring Boot with REST API
Spring Boot with Database and Data JPA
Spring Boot with Kafka
Spring Boot with AOP
In Spring Boot, the CommandLineRunner
interface is used to run a specific piece of code when the application is fully started. This can be useful for tasks like database initialization, data seeding, triggering certain actions immediately after startup, or any other startup-related task.
Implementing the CommandLineRunner
interface requires you to override its run
method, which gets executed after the application context is loaded.
CommandLineRunner
:CommandLineRunner
in your component:@Component public class StartupRunner implements CommandLineRunner { @Override public void run(String... args) throws Exception { System.out.println("This code runs on application startup"); } }
In the example above, the code inside the run
method gets executed when the application starts.
You can also access the application arguments passed in the command line using the args
parameter:
@Override public void run(String... args) throws Exception { for (String arg : args) { System.out.println("Arg: " + arg); } }
CommandLineRunner
Beans:If you have multiple beans implementing CommandLineRunner
, you can control the order of their execution using the Order
annotation or by implementing the Ordered
interface:
@Component @Order(1) public class FirstStartupRunner implements CommandLineRunner { // ... } @Component @Order(2) public class SecondStartupRunner implements CommandLineRunner { // ... }
In the example above, FirstStartupRunner
will execute before SecondStartupRunner
because of the order specified.
The CommandLineRunner
interface is a simple but powerful feature in Spring Boot, allowing developers to execute specific actions after the Spring application context has been fully initialized. It's great for tasks that you want to be done every time the application starts, but be cautious to not overload it with heavy operations that can significantly delay the application's startup time.
How to use Command Line Runner in Spring Boot:
CommandLineRunner
in Spring Boot, implement the interface in a class and override the run
method. Spring Boot will automatically detect and execute it during startup. Example:import org.springframework.boot.CommandLineRunner; import org.springframework.stereotype.Component; @Component public class MyCommandLineRunner implements CommandLineRunner { @Override public void run(String... args) throws Exception { // Your startup logic here } }
Spring Boot CommandLineRunner example:
CommandLineRunner
that prints a message during application startup:@Component public class MyCommandLineRunner implements CommandLineRunner { @Override public void run(String... args) throws Exception { System.out.println("Application started with command-line arguments: " + Arrays.toString(args)); } }
Custom CommandLineRunner in Spring Boot:
CommandLineRunner
by implementing the interface and defining your logic. For instance:@Component public class CustomCommandLineRunner implements CommandLineRunner { @Override public void run(String... args) throws Exception { // Custom logic here } }