Spring Boot FAQ: Top Questions
28. What is the role of CommandLineRunner and ApplicationRunner?
Both are interfaces used to run code after the Spring Boot application has started. They are typically used for initialization logic.
📘 Differences:
CommandLineRunner
accepts raw String arguments.ApplicationRunner
provides access to parsedApplicationArguments
.
📥 Example:
@Component
public class MyRunner implements CommandLineRunner {
public void run(String... args) {
System.out.println("App started!");
}
}
🏆 Expected Output:
"App started!" printed on console after context loads.
🛠️ Use Cases:
- Pre-loading data into database.
- Custom startup logic.