Mastering Spring Bean Lifecycle: 8 Essential Stages Explained

This article walks through the eight critical phases of a Spring bean's lifecycle—from instantiation and property population to initialization, post‑processor hooks, usage, and destruction—detailing how Spring creates, configures, enhances, and finally disposes of beans, with code snippets and diagrams for clarity.

Mike Chen's Internet Architecture
Mike Chen's Internet Architecture
Mike Chen's Internet Architecture
Mastering Spring Bean Lifecycle: 8 Essential Stages Explained

1. Instantiation

In this stage, the Spring container creates the bean instance by invoking its constructor based on configuration or annotations, marking the start of the bean lifecycle.

Instantiation can be performed via:

Constructor instantiation: Spring calls the bean class constructor, the default method.

Static factory method instantiation: If a static factory method is configured, Spring invokes it to obtain the instance.

After instantiation, no properties are set and the bean remains in a raw state.

2. Populate properties

Once instantiated, Spring injects property values into the bean according to configuration or annotations.

Property injection methods include:

Setter injection: Spring calls the bean's setter methods with the appropriate values.

Field injection: Spring directly sets the declared fields.

Constructor injection: Values are passed as constructor arguments.

At this point, the bean has its properties assigned but no initialization methods have run.

3. Initialization

After property injection, if the bean implements InitializingBean or defines an init‑method, Spring invokes it, allowing custom initialization logic.

4. Pre‑initialization post‑processor

Before bean initialization, Spring calls all registered BeanPostProcessor's postProcessBeforeInitialization methods, enabling custom modifications.

Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException

5. Initialization complete

When initialization methods finish, the bean is fully initialized and ready for use.

6. Post‑initialization post‑processor

After initialization, Spring invokes all BeanPostProcessor's postProcessAfterInitialization methods, allowing further enhancements such as logging, AOP proxies, or validation.

Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException;

7. Using

The bean can now be used by other components to perform its business logic.

8. Destruction

When the Spring container shuts down, it calls the bean's destroy method, either via DisposableBean or a configured destroy‑method, to release resources.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaBackend Developmentspringdependency-injectionbean-lifecycle
Mike Chen's Internet Architecture
Written by

Mike Chen's Internet Architecture

Over ten years of BAT architecture experience, shared generously!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.