This article tells what is CommandLineRunner and different ways of implementing it or use it in Spring Boot.
Table of contents:
- 1. What is CommandLineRunner?
- 2. Implementing CommandLineRunner
- 3. @Bean CommandLineRunner
- 4. Download Source Code
- 5. References
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.
- Application starts.
- Spring Boot initializes and configures beans, properties, and the application context.
CommandLineRunner
(orApplicationRunner
) methods are executed.- 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.
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.
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.
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
The post Spring Boot CommandLineRunner Example appeared first on Mkyong.com.