Main Tutorials

Spring Boot CommandLineRunner Example

This article tells what is CommandLineRunner and different ways of implementing it or use it in Spring Boot.

Table of contents:

P.S. Tested with Spring Boot 3.1.2

1. What is CommandLineRunner?

The CommandLineRunner is an interface in Spring Boot. When a class implements this interface, Spring Boot will automatically run its run method after loading the application context. Usually, we use this CommandLineRunner to perform startup tasks like user or database initialization, seeding, or other startup activities.

Here’s a simplified sequence to run CommandLineRunner. in Spring Boot.

  1. Application starts.
  2. Spring Boot initializes and configures beans, properties, and the application context.
  3. CommandLineRunner (or ApplicationRunner) methods are executed.
  4. The application is now ready to serve connections or requests.

2. Implementing CommandLineRunner

2.1 We can create a @Component bean that implements the CommandLineRunner interface and override the run() method. Spring will run the code during the Spring application startup.

DatabaseInitializer.java

package com.mkyong;

import com.mkyong.book.Book;
import com.mkyong.book.BookRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.time.LocalDate;

@Component
public class DatabaseInitializer implements CommandLineRunner {

  @Autowired
  BookRepository bookRepository;

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

      bookRepository.save(
              new Book("Book A",
                      BigDecimal.valueOf(9.99),
                      LocalDate.of(1982, 8, 31))
      );

      System.out.println("Database initialized!");

  }
}

2.2 Another common way is to make the @SpringBootApplication class implements the CommandLineRunner interface directly.

StartApplication2.java

package com.mkyong;

import com.mkyong.book.Book;
import com.mkyong.book.BookRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.math.BigDecimal;
import java.time.LocalDate;

@SpringBootApplication
public class StartApplication2 implements CommandLineRunner {

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

  @Autowired
  BookRepository bookRepository;

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

      bookRepository.save(
              new Book("Book A",
                      BigDecimal.valueOf(9.99),
                      LocalDate.of(1982, 8, 31))
      );

      System.out.println("Database initialized!");


  }
}

3. @Bean CommandLineRunner

It’s also common for the developer to annotate the CommandLineRunner as a @Bean; Spring will also run the code during the startup of the Spring application.

StartApplication.java

package com.mkyong;

import com.mkyong.book.Book;
import com.mkyong.book.BookRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

import java.math.BigDecimal;
import java.time.LocalDate;

@SpringBootApplication
public class StartApplication {

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

  @Autowired
  BookRepository bookRepository;

  @Bean
  public CommandLineRunner startup() {

      return args -> {

          bookRepository.save(
                  new Book("Book A",
                          BigDecimal.valueOf(9.99),
                          LocalDate.of(1982, 8, 31))
          );

          System.out.println("Database initialized!");

      };

  }
}

4. Download Source Code

$ git clone https://github.com/mkyong/spring-boot.git

$ cd spring-boot-commandlinerunner

$ ./mvnw spring-boot:run

5. References

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
1 Comment
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Charan S
1 month ago

Thanks.. very easy to follow tutorial with simple examples (in a good way).