Backend Development 9 min read

Deep Dive into Spring Bean Lifecycle: Property Assignment, Aware Callbacks, Initialization & Destruction

This article continues the Spring bean lifecycle series by explaining the property‑assignment stage, Aware interface callbacks, bean initialization phases, and the destruction process, illustrating each step with source‑code diagrams and highlighting how BeanPostProcessor extensions drive the lifecycle.

Sanyou's Java Diary
Sanyou's Java Diary
Sanyou's Java Diary
Deep Dive into Spring Bean Lifecycle: Property Assignment, Aware Callbacks, Initialization & Destruction

This article continues the previous post on Spring bean creation and now examines the remaining lifecycle stages from a source‑code perspective, covering property assignment, Aware interface callbacks, initialization, and destruction.

5. Spring Bean Property Assignment Phase

1) Pre‑assignment stage

In this stage Spring injects annotations such as @Autowired and @Resource . The PropertyValues API encapsulates the properties parsed from XML configuration.

The AutowiredAnnotationBeanPostProcessor handles @Autowired and @Value , while CommonAnnotationBeanPostProcessor implements @PostConstruct , @PreDestroy , and @Resource . Developers can also implement their own postProcessProperties to modify PropertyValues before injection.

2) Assignment stage

Spring copies the values from PropertyValues into the bean fields, performing type conversion, placeholder resolution, and other features as needed.

6. Aware Interface Callback Phase

If a bean implements any of the Aware interfaces, Spring invokes the corresponding methods during this phase. The populateBean method finishes and initializeBean begins, which calls invokeAwareMethods .

7. Spring Bean Initialization Phase

1) Before initialization

BeanPostProcessor callbacks are still invoked. The ApplicationContextAwareProcessor checks for Aware interfaces and triggers their methods.

2) Initialization

Spring calls invokeInitMethods . If the bean implements InitializingBean or defines an initMethod , those methods are invoked.

3) After initialization

BeanPostProcessor callbacks run again, and the AspectJAwareAdvisorAutoProxyCreator creates any required dynamic proxies.

At this point a singleton bean is fully created and stored in the singletonObjects cache for future retrieval.

8. Spring Bean Destruction Phase

This phase runs when the Spring container shuts down. Beans are wrapped as DisposableBeanAdapter objects and placed in the disposableBeans map.

The container calls destroySingleton on DefaultSingletonBeanRegistry , which retrieves the registered DisposableBean and invokes its destroy method. BeanPostProcessor callbacks are also triggered during this stage.

After all destruction callbacks complete, the bean is fully removed.

9. Full Summary

The article completes the source‑code analysis of the Spring bean lifecycle, covering creation, property injection, Aware callbacks, initialization, and destruction. It shows how BeanPostProcessor extensions are used at each stage and encourages readers to implement custom processors for their own needs.

10. Thought Questions

What other Spring features are implemented via BeanPostProcessor extensions, and which external frameworks integrate with Spring using similar mechanisms?

JavaBackend DevelopmentSpringdependency injectionSpring Frameworkbean lifecycle
Sanyou's Java Diary
Written by

Sanyou's Java Diary

Passionate about technology, though not great at solving problems; eager to share, never tire of learning!

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.