Backend Development 10 min read

Understanding the Spring Bean Lifecycle: From Instantiation to Destruction

This article explains the complete Spring Bean lifecycle—including IoC fundamentals, bean creation, property injection, initialization callbacks, custom init/destroy methods, and post‑processor hooks—by walking through detailed source‑code examples and step‑by‑step execution traces for a sample bean.

IT Services Circle
IT Services Circle
IT Services Circle
Understanding the Spring Bean Lifecycle: From Instantiation to Destruction

Spring's Inversion of Control (IoC) container manages object creation and dependency injection, reversing the traditional "new" approach so that the container, not the object, controls its lifecycle. The article first introduces IoC concepts, emphasizing the "Hollywood principle" ("Don’t call us, we’ll call you") and clarifies what is controlled, what is reversed, and why.

The core of the discussion is the Bean lifecycle for singleton beans, which consists of instantiation, property assignment, initialization (including aware interfaces, BeanPostProcessor pre‑processing, InitializingBean callbacks, custom init methods, and post‑processing), and finally destruction (DisposableBean and custom destroy methods).

To illustrate these steps, the author provides a concrete example bean public class LouzaiBean implements InitializingBean, BeanFactoryAware, BeanNameAware, DisposableBean { ... } with constructor, setter, and lifecycle‑related method implementations that print messages reflecting each stage.

A custom public class MyBeanPostProcessor implements BeanPostProcessor { ... } is defined to log before‑ and after‑initialization callbacks, demonstrating how post‑processors integrate into the lifecycle.

The Spring XML configuration registers both beans: <bean name="myBeanPostProcessor" class="demo.MyBeanPostProcessor"/> <bean name="louzaiBean" class="demo.LouzaiBean" init-method="init" destroy-method="destroyMethod"> <property name="name" value="楼仔"/> </bean>

A test class launches the context and triggers the bean: public class MyTest { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml"); LouzaiBean louzaiBean = (LouzaiBean) context.getBean("louzaiBean"); louzaiBean.work(); ((ClassPathXmlApplicationContext) context).destroy(); } }

The console output shows the exact order of method invocations, from constructor to property setting, BeanNameAware, BeanFactoryAware, BeanPostProcessor pre‑processing, InitializingBean, custom init, BeanPostProcessor post‑processing, bean usage, and finally the destroy sequence, confirming the lifecycle diagram.

The article then breaks down the source‑code flow: entry via doCreateBean() , instance creation with createBeanInstance() , property population via populateBean() , initialization steps in initializeBean() (aware methods, post‑processor hooks, init callbacks), and destruction through destroy() and custom destroy method.

Key takeaways are summarized: doCreateBean() is the entry point; createBeanInstance() constructs the object; populateBean() injects dependencies; initializeBean() orchestrates aware interfaces, post‑processor pre‑/post‑hooks, InitializingBean, and custom init; and destroy() runs DisposableBean followed by the custom destroy method.

Finally, the author encourages readers to explore other Spring source components such as dependency injection and circular dependencies, and invites feedback on further topics of interest.

backendJavaIoCSpringdependency injectionbean lifecycle
IT Services Circle
Written by

IT Services Circle

Delivering cutting-edge internet insights and practical learning resources. We're a passionate and principled IT media platform.

0 followers
Reader feedback

How this landed with the community

login 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.