Spring Boot CommandLineRunner Example
Spring Command Runner is an interface in the Spring Framework that allows developers to execute code upon the startup of a Spring application. Let us delve into a practical example of Spring Boot CommandLineRunner usage.
1. Understanding CommandLineRunner
CommandLineRunner is an interface provided by the Spring Boot framework. It’s used to execute the code after the Spring Boot application has started up and the application context has been created. This interface has a single-run method:
public interface CommandLineRunner { void run(String... args) throws Exception; }
Developers can implement this interface to perform any tasks they want to execute at the startup of their Spring Boot application. These tasks could include database initialization, cache warming, or any custom logic required at application startup.
Here’s how the CommandLinerRunner works:
- Implementation: Developers create a class that implements the CommandLineRunner interface provided by Spring Boot.
- Override Run Method: Within the implementing class, developers override the run method. This method will be automatically executed by Spring Boot when the application context is ready.
- Execute Custom Logic: Developers write the custom code they want to execute during application startup inside the run method. This code could include tasks like database initialization, cache warming, or any other initialization logic.
- Automatic Invocation: Spring Boot automatically detects all CommandLineRunner beans in the application context and invokes their run methods in the order of their priority. Developers can control the order of execution using the @Order annotation or by implementing the Ordered interface.
1.1 Implementing CommandLineRunner
Here’s a basic example of how to use CommandLineRunner:
import org.springframework.boot.CommandLineRunner; import org.springframework.stereotype.Component; @Component public class MyCommandLineRunner implements CommandLineRunner { @Override public void run(String... args) throws Exception { System.out.println("Executing tasks after application startup..."); // Perform startup tasks here } }
In this example, MyCommandLineRunner is a Spring-managed component that implements the CommandLineRunner interface. The run method will be invoked automatically by Spring Boot once the application context is ready.
CommandLineRunner provides a convenient way to execute code during application startup in a Spring Boot application, ensuring that specific tasks are carried out reliably and efficiently.
2. @Bean CommandLineRunner in Spring
In Spring Framework, @Bean
is an annotation used to declare a single bean, a method that returns a bean instance, within a Spring configuration class. CommandLineRunner
is an interface provided by Spring Boot, used to execute custom code after the application context has been initialized. When you use @Bean
to define a CommandLineRunner
bean in your Spring configuration class, you are effectively registering a component that will be invoked by Spring Boot upon application startup.
Here’s how it works:
- Define a Method: Annotate a method in your Spring configuration class with
@Bean
that returns aCommandLineRunner
instance. - Override Run Method: Implement the
run
method within the method body. This method will contain the code you want to execute after application startup. - Automatic Invocation: Spring Boot automatically detects all
CommandLineRunner
beans in the application context and invokes theirrun
methods in the order of their priority.
Here’s a basic example of using @Bean
to define a CommandLineRunner
:
import org.springframework.boot.CommandLineRunner; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class MyConfiguration { @Bean public CommandLineRunner myCommandLineRunner() { return new CommandLineRunner() { @Override public void run(String... args) throws Exception { System.out.println("Executing tasks after application startup..."); // Perform startup tasks here } }; } }
In this example, the method myCommandLineRunner
is annotated with @Bean
to declare a CommandLineRunner
bean. The run
method within the anonymous class implementation will be invoked automatically by Spring Boot upon application startup.
@Bean
CommandLineRunner provides a flexible way to define and register components that execute code during application startup in a Spring Boot application.
3. Conclusion
CommandLineRunner in Spring, whether implemented as a component, with @Bean
, or by directly implementing the interface, offers a powerful mechanism for executing custom code after the initialization of a Spring Boot application. It provides developers with a straightforward way to perform tasks like database setup, cache warming, or any other initialization logic required at application startup. Whether through the automatic detection of CommandLineRunner beans or through explicit declaration using @Bean
, Spring Boot ensures the reliable execution of these tasks in the order specified or inferred. By leveraging CommandLineRunner, developers can ensure that their Spring Boot applications start up smoothly and are ready to serve their purpose effectively.
Happy coding!