Sitemap
Alice Dai

Alice Dai

Software engineer

Spring Boot lifecycle

2 min readSep 12, 2025

--

Spring Boot lifecycle = bootstrap → context creation → bean definition → dependency injection → initialization → ready → shutdown.

Get Alice Dai’s stories in your inbox

Join Medium for free to get updates from this writer.

The Spring Boot lifecycle describes how a Spring Boot application starts, runs, and shuts down. It builds on the Spring Framework’s lifecycle, but with extra automation and conventions. Here’s a clear breakdown:

1. Startup Phase

Launch with SpringApplication.run()

  • The entry point (your main method) calls SpringApplication.run(...).
  • Bootstraps logging, sets up environment variables, and creates the ApplicationContext (the IoC container).

Environment Preparation

  • Reads properties from application.properties or application.yml.
  • Profiles (spring.profiles.active) and externalized configuration are applied.

ApplicationContext Creation

  • Instantiates the IoC container (e.g., AnnotationConfigApplicationContext).
  • Registers beans, components, and configurations (@Component, @Configuration, @Bean).

Bean Definition Loading

  • Classpath scanning finds candidate beans.
  • Beans are defined and metadata is loaded.

2. Dependency Injection (DI) & Bean Lifecycle

Bean Instantiation

  • Creates bean objects (default scope: singleton).

Dependency Injection

  • Injects dependencies using:

Constructor injection (recommended for immutability)

  • Setter injection
  • Field injection

Post-Processing

  • BeanPostProcessors modify beans (e.g., proxy wrapping for @Transactional or @Async).

Initialization

  • @PostConstruct, InitializingBean.afterPropertiesSet(), or custom init methods run.

Ready-to-use Beans

  • Beans are placed in the context and can be used by other components.

3. Application Ready Phase

Context Refresh Complete

  • Spring publishes lifecycle events (ContextRefreshedEvent, ApplicationReadyEvent).
  • CommandLineRunner and ApplicationRunner beans are executed.

Application is Running

  • Your REST controllers, services, repositories, etc. are available.
  • Auto-configurations (like embedded Tomcat/Jetty) start listening for requests.

4. Shutdown Phase

Context Close

  • Triggered when the JVM shuts down or you call context.close().
  • Spring publishes a ContextClosedEvent.

Bean Destruction

  • Executes cleanup callbacks:
  • @PreDestroy
  • DisposableBean.destroy()
  • Custom destroy methods in @Bean(destroyMethod="...").

Resources Released

  • Database connections, thread pools, caches, etc. are cleaned up.

--

--