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.
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 BeansException5. 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.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Mike Chen's Internet Architecture
Over ten years of BAT architecture experience, shared generously!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
