Enterprise applications need fine-grained control over object lifecycle — when objects are created, how they’re initialized, what happens after all dependencies are set, and how they’re cleaned up. Different use cases require different sharing strategies (singleton for stateless services, prototype for stateful objects).
The Spring Bean Lifecycle defines the birth-to-death phases every managed bean goes through: instantiation → dependency injection → initialization → ready → destruction. Bean scopes determine how long a bean lives and how many instances are created. Custom scopes can be defined for special use cases.
- Instantiation: Container creates the bean instance via constructor
- Dependency injection: Container injects dependencies (via constructor, setter, or field)
- Awareness callbacks: If bean implements
BeanNameAware,BeanFactoryAware,ApplicationContextAware, container calls them - Pre-initialization:
BeanPostProcessor.postProcessBeforeInitialization()— container-wide hooks - Initialization:
@PostConstructmethod →InitializingBean.afterPropertiesSet()→ custominit-method - Post-initialization:
BeanPostProcessor.postProcessAfterInitialization()— proxy creation happens here (AOP) - Ready: Bean is fully initialized and available for use
- Destruction:
@PreDestroymethod →DisposableBean.destroy()→ customdestroy-method - Scopes: singleton (one per container), prototype (new per request), request/ session/application (web contexts), custom
- Singleton scope: Default; one instance per IoC container; ideal for stateless services
- Prototype scope: New instance every time the bean is requested; use for stateful objects
- Web scopes: request (one per HTTP request), session (one per HTTP session), application (one per ServletContext)
- Custom scopes: Define custom scope logic (e.g., thread-scoped, tenant-scoped) via
Scopeinterface - BeanPostProcessor: Container-wide hooks that run for every bean — critical for AOP proxy creation
- Lifecycle callbacks: Three ways per phase: annotations (@PostConstruct/@PreDestroy), interfaces, custom init/destroy method
- Built from: Spring IoC Container — The container manages the entire bean lifecycle
- Built from: Spring Framework — Lifecycle management is a core Spring feature
- Related: Spring Autowiring — Dependency injection occurs during the lifecycle’s second phase
- Related: Stateless Bean Lifecycle — EJB containers also manage bean lifecycles, but with different phases
- Contrasts with: Stateful Bean Lifecycle — EJB stateful beans have passivation/activation; Spring beans don’t
- Prototype destruction: Container does NOT call destroy() on prototype beans — you must clean them up manually
- Circular dependency: Constructor injection + circular dependency causes BeanCurrentlyInCreationException; use setter injection or @Lazy
- PostConstruct in proxy:
@PostConstructin a proxy-wrapped bean runs on the target, not the proxy - Scope mismatch: Injecting a shorter-lived bean (request) into a longer-lived bean (singleton) requires scoped proxy (
@Scope(proxyMode=ScopedProxyMode.TARGET_CLASS))