Spring Boot Startup Sequence (SpringApplication.run Phases)
Fast recall
Quick revision
Core answer
SpringApplication.run() orchestrates a multi-phase startup: it prepares the environment, creates and refreshes the `ApplicationContext`, then calls runners and publishes a final `ApplicationReadyEvent`. Each phase is observable via SpringApplicationRunListeners and typed ApplicationEvents. The order matters for your custom logic: @PostConstruct runs during bean initialization inside refresh; CommandLineRunner and ApplicationRunner run after refresh and the embedded server has bound its port; ApplicationReadyEvent fires when the app is ready to accept traffic.Whether the candidate can trace the lifecycle beyond 'the context starts' and explain where things like property loading and bean initialization happen
Confusing `ApplicationRunner`/`CommandLineRunner` (called after context is ready) with `@PostConstruct` (called during bean initialization inside context refresh)
Mention `SpringApplicationRunListeners` and the exact `ApplicationEvent` types (`ApplicationStartingEvent`, `ApplicationEnvironmentPreparedEvent`, etc.) and where to hook custom logic safely
Interview-ready guide
Interview answer
Spring Boot's startup follows a precise sequence that determines when each hook is safe to use. Getting the order wrong means initialization code runs before its dependencies are ready.
The sequence
ApplicationStartingEvent→ environment prep →ApplicationEnvironmentPreparedEvent→ context creation → bean definition load →ApplicationPreparedEvent→ refresh- During refresh: bean instantiation, dependency injection,
@PostConstruct,BeanPostProcessors, embedded server start ApplicationStartedEventfires after refresh- Runners (
CommandLineRunner,ApplicationRunner) execute next ApplicationReadyEventmarks the application as ready for traffic
What goes where
@PostConstruct: initialization that depends only on injected collaborators — cache warmup, connection validation- Runners: startup logic that should run after the full context is ready — data seeding, integration checks, feature flag registration
@EventListener(ApplicationReadyEvent.class): same timing as runners but declared as an event listener rather than implementing an interfaceEnvironmentPostProcessor: add property sources from external systems (Vault, AWS Secrets Manager) before any bean reads the environment
Multiple runners
- Execute in
@Ordersequence — lower value runs first CommandLineRunnerreceives rawString[] args;ApplicationRunnerreceives a typedApplicationArgumentsobject — the only practical difference
Failure semantics
- Exception during refresh →
ApplicationFailedEventand JVM exits - Exception in a runner →
ApplicationFailedEvent; server already bound the port (load balancer briefly sees an open port that immediately closes)
In an interview, the strong answer is: `@PostConstruct` for per-bean init, runners or `ApplicationReadyEvent` for context-wide init, `EnvironmentPostProcessor` for early property injection — pick the hook by what state must already exist when it fires.
Spring Boot Startup Sequence (SpringApplication.run Phases)
Direct Answer
SpringApplication.run() orchestrates a multi-phase startup: it prepares the environment, creates and refreshes the ApplicationContext, then calls runners and publishes a final ApplicationReadyEvent. Each phase is observable via SpringApplicationRunListeners and typed ApplicationEvents. The order matters for your custom logic: @PostConstruct runs during bean initialization inside refresh; CommandLineRunner and ApplicationRunner run after refresh and the embedded server has bound its port; ApplicationReadyEvent fires when the app is ready to accept traffic.
Common Mistakes & Pitfalls
Confusing ApplicationRunner/CommandLineRunner (called after context is ready) with @PostConstruct (called during bean initialization inside context refresh)
Check your understanding
Answer these without looking back at the article.
- Where is the best place to run logic that needs the full application context wired but should complete before the app accepts traffic?
- How would you measure which beans are slowest to initialize during startup?
- What is the difference between `ApplicationStartedEvent` and `ApplicationReadyEvent`?
- How does the readiness probe in Kubernetes use `ApplicationReadyEvent`?
Related interview prep categories
1Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.