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.
Introduction
Spring Boot gives you several hooks for running code during or after application startup, but the right hook depends on what "after startup" actually means in your application. Some code should run once the application context is ready, while other code should wait until the app is fully started and ready to serve requests.
Use CommandLineRunner or ApplicationRunner for Startup Tasks
The most common startup hooks are CommandLineRunner and ApplicationRunner. Both run after the application context has been created.
CommandLineRunner is simple and gives you raw argument strings. ApplicationRunner is similar, but it exposes parsed ApplicationArguments.
If you care about command-line options, ApplicationRunner is usually more expressive.
Use ApplicationReadyEvent When You Need Full Readiness
Sometimes you do not just want the context initialized. You want the application to be fully started and ready. In that case, listening for ApplicationReadyEvent is often the better hook.
This is a good place for tasks such as warm-up calls, readiness notifications, or deferred initialization that should happen only after the app has reached its running state.
Choose the Hook Based on the Work
A practical guideline is:
- use
CommandLineRunnerorApplicationRunnerfor one-time startup logic tied to bootstrapping - use
ApplicationReadyEventfor work that should happen only when the app is fully up - avoid putting heavy blocking logic in early startup if it delays application availability unnecessarily
That distinction matters for operational behavior. A heavy startup runner can slow down deployments, health checks, and container readiness.
Order and Failure Behavior Matter
If multiple runners exist, ordering can matter. Spring lets you use @Order or implement Ordered so one runner executes before another.
Also remember that exceptions thrown from startup code can fail the application startup entirely. That may be exactly what you want for critical initialization, but it should be a deliberate decision.
Keep Startup Work Small and Observable
Startup hooks are tempting places to hide miscellaneous logic. Resist that. If the application must load caches, seed data, or contact external systems, log clearly, measure duration, and decide whether failures should stop boot or merely warn.
This makes startup behavior operationally understandable instead of mysterious.
If the startup work is slow but noncritical, consider handing it off to a background mechanism rather than tying it directly to startup readiness. That keeps deployments faster while still letting the task run under controlled logging and monitoring.
Common Pitfalls
Using a runner for long blocking work can make the application appear hung during deployment.
Confusing "context created" with "application fully ready" leads to the wrong lifecycle hook.
Packing many unrelated tasks into one large startup method makes failures harder to isolate.
Summary
- Use
CommandLineRunnerorApplicationRunnerfor ordinary startup tasks. - Use
ApplicationReadyEventwhen the code should run only after full application readiness. - Keep startup work intentional, observable, and appropriately ordered.
- Decide explicitly whether startup-task failure should stop the whole application.

