Java and Spring Level 3 This level · 45 of 56
Hard java spring

What is the exact startup flow of a Spring Boot application?

The 30 second answer

SpringApplication.run first sets up bootstrap support and startup listeners, then prepares the Environment and loads configuration. After printing the banner, it creates and prepares the appropriate ApplicationContext. Refresh processes configuration and bean definitions, creates non-lazy singletons and starts an embedded server for web applications. Spring Boot then publishes ApplicationStartedEvent, runs ApplicationRunner and CommandLineRunner beans, and finally publishes ApplicationReadyEvent if startup succeeds. So started and ready aren't the same milestone.

Could you say this in an interview?

The whole flow is visible in one method:

DefaultBootstrapContext bootstrapContext = createBootstrapContext();
SpringApplicationRunListeners listeners = getRunListeners(args);
listeners.starting(bootstrapContext, this.mainApplicationClass);
try {
    ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
    ConfigurableEnvironment environment = prepareEnvironment(listeners, bootstrapContext, applicationArguments);
    Banner printedBanner = printBanner(environment);
    context = createApplicationContext();
    prepareContext(bootstrapContext, context, environment, listeners, applicationArguments, printedBanner);
    refreshContext(context);
    afterRefresh(context, applicationArguments);
    listeners.started(context, timeTakenToStarted);
    callRunners(context, applicationArguments);
}
catch (Throwable ex) {
    throw handleRunFailure(context, ex, listeners);
}
if (context.isRunning()) {
    listeners.ready(context, startup.ready());
}

SpringApplication.run L292-L330 (shortened)

Step by step:

  1. **Constructor (before run).** SpringApplication deduces the web application type from the classpath and loads initializers and listeners from spring.factories.
  2. Starting. ApplicationStartingEvent is published. Almost nothing exists yet; logging is initialized here.
  3. Environment. prepareEnvironment creates the Environment, adds command-line arguments, and lets the config-data system load application.properties/.yml, profile files and imports. ApplicationEnvironmentPreparedEvent is published.
  4. Context creation. The context type depends on the web type: servlet, reactive or plain.
  5. Prepare. Initializers run (ApplicationContextInitializedEvent), the main class is registered as a bean source, then ApplicationPreparedEvent.
  6. Refresh (Spring Framework's AbstractApplicationContext.refresh()): bean factory post-processors run, which parse @Configuration classes, component scanning and auto-configuration imports; bean post-processors are registered; for web contexts onRefresh() creates the embedded server; all non-lazy singletons are created; ContextRefreshedEvent is published and the web server starts listening.
  7. Started. ApplicationStartedEvent, then AvailabilityChangeEvent with LivenessState.CORRECT.
  8. Runners. All ApplicationRunner and CommandLineRunner beans run, in @Order order.
  9. Ready. ApplicationReadyEvent, then ReadinessState.ACCEPTING_TRAFFIC.

If anything throws, handleRunFailure publishes ApplicationFailedEvent, reports the failure and closes the context.

Interview distinction: ContextRefreshedEvent (Spring Framework) fires before runners; ApplicationReadyEvent (Spring Boot) fires after them. Use ApplicationReadyEvent for "the app is fully up" work.

Alternate interview scenarios

"Spring Boot startup is taking 45 seconds. How do you diagnose and fix it?"

I measure before I change anything. Spring Boot can record startup steps if I set a BufferingApplicationStartup on SpringApplication, and the Actuator startup endpoint then shows each bean's creation time, so the slow beans are visible by name. Usually it is one of these: a bean doing I/O in its constructor or init method, such as warming a cache or calling another service; Flyway or Liquibase migrations; Hibernate validating or updating the schema; a connection pool that waits for a slow database; or wide component scanning with many unused auto-configurations. The fixes follow the finding. I move warm-up to a background task after startup, run migrations as a separate step, turn off schema validation in production, narrow the scan and exclude unneeded auto-configuration. spring.main.lazy-initialization hides the cost from startup, but it moves the cost and the errors to the first request.

Sources (4)
  1. Spring Boot reference: Application events and listenersdocs.spring.io
  2. Spring Boot reference: Application availabilitydocs.spring.io
  3. Spring Boot reference: SpringApplication (application startup tracking, lazy initialization)docs.spring.io
  4. Spring Boot reference: Actuator endpoints (startup)docs.spring.io
Previous
Next