In a Spring Boot application, you can execute code after the application has started by implementing the ApplicationRunner
or CommandLineRunner
interfaces. These interfaces provide a way to run code at the end of the application startup process.
Below, here’s how you can use them.
Using ApplicationRunner
:
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
@Component
public class MyApplicationRunner implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) throws Exception {
// Your code to be executed after Spring Boot startup
System.out.println("Executing code after Spring Boot startup using ApplicationRunner");
}
}
Using 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 {
// Your code to be executed after Spring Boot startup
System.out.println("Executing code after Spring Boot startup using CommandLineRunner");
}
}
Both ApplicationRunner
and CommandLineRunner
interfaces have a run
method that gets called when the Spring Boot application starts. You can use these interfaces based on your preference.
Remember to annotate the class with @Component
to ensure that Spring discovers and manages it.
Now, when you start your Spring Boot application, the code inside the run
method of your ApplicationRunner
or CommandLineRunner
implementation will be executed after the application has started.
What is the difference between ApplicationRunner
and CommandLineRunner
?
Both ApplicationRunner
and CommandLineRunner
are interfaces in Spring Boot that provide a way to execute code after the Spring Boot application has started. They are used to perform tasks or initialization steps that should occur once the application context is fully initialized.
The main difference between them lies in the method signature of the run
method:
- ApplicationRunner:
- Method Signature:
void run(ApplicationArguments args) throws Exception;
ApplicationArguments
is a wrapper around the command-line arguments. It provides access to both the raw String arguments and parsed arguments.- This interface is suitable if you need access to command-line arguments or if you want to work with application arguments in a more structured way.
@Override public void run(ApplicationArguments args) throws Exception { // Your code here }
- Method Signature:
- CommandLineRunner:
- Method Signature:
void run(String... args) throws Exception;
- Takes a variable number of String arguments representing the command-line arguments.
- This interface is simpler and more straightforward if you only need access to the raw command-line arguments as strings.
@Override public void run(String... args) throws Exception { // Your code here }
- Method Signature:
In a nutshell, if you need more structured access to command-line arguments, or if you want to perform tasks that involve the parsing of command-line arguments, you might prefer ApplicationRunner
. If you simply need access to the raw command-line arguments as strings, CommandLineRunner
is a simpler choice. Choose the one that fits your specific use case and coding preferences.