Running code after Spring Boot starts
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
When working with a Spring Boot application, there might be specific tasks or operations that you would like to execute once the application context is fully initialized and the application is ready to service requests. These tasks can range from preloading data, initializing caches, or starting background jobs. This article delves into various mechanisms provided by Spring Boot to run code after the application has started.
Runnable Interface
The simplest way to execute code after your Spring Boot application has started is to implement the Runnable interface within a @Component. Spring Boot will automatically detect and run any beans implementing the Runnable interface using a CommandLineRunner or ApplicationRunner.
Using CommandLineRunner
CommandLineRunner is triggered once the application context has been loaded. It provides access to the application's command-line arguments.
Using ApplicationRunner
If you need to customize how command-line arguments are handled, use the ApplicationRunner interface. It employs the ApplicationArguments class, which provides a more structured way to retrieve arguments.
Using @EventListener for ApplicationReadyEvent
Another common approach to execute code after the application starts is to listen for the ApplicationReadyEvent. This event is fired once the application is completely initialized.
Scheduling Tasks with @Scheduled
For recurring tasks or when your startup logic needs to be executed periodically, consider using Spring's @Scheduled annotation. Tasks can be scheduled with fixed delays, fixed rates, or cron expressions.
First, ensure you have enabled scheduling in your application by adding @EnableScheduling in one of your configuration classes.
Key Points Summary
| Mechanism | Purpose | Example Use Case |
CommandLineRunner | Execute immediately after the application context is loaded using command-line arguments. | Preloading initial data. |
ApplicationRunner | Similar to CommandLineRunner but offers more structured argument handling. | More complex argument-dependent tasks. |
ApplicationReadyEvent | Execute code once the application is fully ready to process requests. | Initialize services needing full startup. |
@Scheduled | Execute repetitive tasks using fixed rates or cron expressions. | Periodic data clean-up or maintenance. |
Additional Considerations
- Asynchronous Execution: If the startup task is time-consuming and should not block the application’s readiness, consider using
@Asyncto run the code asynchronously. Ensure that@EnableAsyncis added to one of the configuration classes.
- Transaction Management: For database operations during startup, ensure proper transaction management. Use
@Transactionalto ensure consistency and rollback capabilities. - Error Handling: Implement adequate error handling and logging mechanisms during startup to handle and trace issues effectively.
- Profile Specific Initialization: Utilize Spring profiles to conditionally run startup tasks relevant to different environments (e.g., dev, test, prod).
By understanding these mechanisms and utilizing them appropriately, you can ensure that your Spring Boot application efficiently initializes any necessary services or configurations, ultimately leading to smoother application operations.

