0 day streak
java / beginner
Snippet

Running Code at Startup

Implementing CommandLineRunner allows you to execute code immediately after the Spring application context is loaded and the app starts.

snippet.java
java
1
2
3
4
5
6
7
@Component
public class StartupApp implements CommandLineRunner {
@Override
public void run(String... args) {
System.out.println("Application has started!");
}
}
spring
Breakdown
1
implements CommandLineRunner
An interface used to indicate that a bean should run when it is contained within a SpringApplication.
2
run(String... args)
The method where your custom startup logic is placed.