Bean Lifecycle and Scopes

What happens to a Spring bean from definition to destruction: instantiation, injection, Aware callbacks, BeanPostProcessors, @PostConstruct and InitializingBean, SmartLifecycle, @PreDestroy and graceful shutdown; plus singleton, prototype, request, session and application scopes, scoped proxies, and thread-safety rules.

On this page

Simple definition#

The bean lifecycle is the sequence of steps Spring performs to create, initialise, use and destroy a bean. A bean’s scope decides how many instances exist and how long each lives.

The problem#

Opening connections in a constructor before properties are injected, forgetting to close thread pools on shutdown, or storing per-user data in a singleton are common, serious bugs. Knowing the lifecycle tells you where code belongs; knowing scopes tells you what is shared.

The lifecycle#

Open the interactive diagram: Spring singleton bean lifecycle Standalone page with zoom, light/dark themes and SVG/PNG export, generated with Archify. Diagram source.

#StepHook you can use
1Instantiate (constructor; dependencies injected here with constructor injection)Constructor
2Populate properties (setter and field injection)—
3Aware callbacksBeanNameAware, BeanFactoryAware, ApplicationContextAware
4BeanPostProcessor.postProcessBeforeInitializationFramework hooks
5Initialisation: @PostConstruct → InitializingBean.afterPropertiesSet() → @Bean(initMethod)Validate config, warm caches
6BeanPostProcessor.postProcessAfterInitializationAOP proxies are created here
7Bean in use. SmartLifecycle.start() for components that run (consumers, schedulers)SmartLifecycle
8On shutdown: SmartLifecycle.stop() → @PreDestroy → DisposableBean.destroy() → destroyMethodRelease resources
@Component
class ExchangeRateCache implements SmartLifecycle {
    private final RatesClient client;
    private volatile boolean running;
    ExchangeRateCache(RatesClient client) { this.client = client; }       // 1: dependencies

    @PostConstruct void validate() { /* check configuration, fail fast */ } // 5
    @Override public void start()  { warmUp(); running = true; }          // 7: after the context is ready
    @Override public void stop()   { running = false; }                   // 8: graceful stop
    @Override public boolean isRunning() { return running; }
    @PreDestroy void close() { client.close(); }                          // 8: release
}

Graceful shutdown (Spring Boot): server.shutdown=graceful (the default since Boot 3.4) stops accepting new requests and lets in-flight ones finish within spring.lifecycle.timeout-per-shutdown-phase. SmartLifecycle beans stop in reverse phase order.

Scopes#

ScopeInstancesLives forTypical use
singleton (default)One per containerThe container’s lifeStateless services, repositories, clients
prototypeNew one per injection or getBeanUntil garbage collected; Spring does not call destroyStateful, short-lived helpers
requestOne per HTTP requestThe requestRequest context data
sessionOne per HTTP sessionThe sessionUser session state (use sparingly)
applicationOne per ServletContextThe web appRarely needed
websocketOne per WebSocket sessionThe sessionWebSocket state
CustomYour rules—For example a thread or tenant scope

Singleton beans must be thread-safe#

A singleton serves all requests concurrently. Keep it stateless: only final, injected dependencies and immutable configuration. Put per-request data in method parameters and local variables.

@Service
class PriceService {
    private BigDecimal lastPrice;        // ❌ shared mutable state: a race between requests
    BigDecimal quote(String sku) { ... }
}

Injecting a shorter-lived bean into a singleton#

A singleton is created once, so injecting a request-scoped or prototype bean directly gives it one instance forever. Solutions:

// 1. Scoped proxy: inject a proxy that resolves the current request's instance on each call
@Component @RequestScope           // proxyMode = TARGET_CLASS by default
class RequestContext { String tenantId; }

// 2. ObjectProvider: ask for a fresh prototype when needed
@Service
class ReportService {
    private final ObjectProvider<ReportBuilder> builders;
    ReportService(ObjectProvider<ReportBuilder> builders) { this.builders = builders; }
    Report build() { return builders.getObject().withDefaults().build(); }  // new prototype each time
}

The other option is @Lookup method injection, where Spring overrides an abstract method to return a new instance.

Lazy initialisation#

  • @Lazy creates a bean on first use instead of at startup.
  • spring.main.lazy-initialization=true makes everything lazy: faster startup, but configuration errors appear later at runtime. That is useful in development, risky in production.

Common mistakes#

Quick check#

In what order do initialisation callbacks run? @PostConstruct, then afterPropertiesSet(), then the custom initMethod. All run after injection and before postProcessAfterInitialization.

At which lifecycle step are AOP proxies created? In BeanPostProcessor.postProcessAfterInitialization.

What is the default scope, and what does it imply? Singleton: one shared instance per container, so it must be stateless or thread-safe.

How do you use a request-scoped bean from a singleton? Inject a scoped proxy (the default for @RequestScope), or look it up through ObjectProvider.

Does Spring call @PreDestroy on prototype beans? No. The container hands prototypes over and does not track their destruction.

Where should long startup work go? In SmartLifecycle.start() or an ApplicationReadyEvent listener, not the constructor.

Summary#

  • Lifecycle: instantiate → inject → Aware → init callbacks → proxies → use → stop → destroy.
  • SmartLifecycle and graceful shutdown handle running components cleanly.
  • Singleton is the default: keep it stateless. Use scoped proxies or ObjectProvider for shorter scopes.

References#