Backend Development 24 min read

Deep Dive into Spring Bean Lifecycle and Initialization Process

This article provides a comprehensive analysis of Spring's bean lifecycle, detailing the refresh method steps, post‑processor ordering, bean creation, dependency injection via @Autowired and @Resource, Aware callbacks, and initialization hooks such as @PostConstruct and init‑method.

Top Architect
Top Architect
Top Architect
Deep Dive into Spring Bean Lifecycle and Initialization Process

Introduction: The article reviews the Spring container refresh process, focusing on how BeanDefinition objects are processed and how various post‑processor extensions are invoked.

It explains the refresh() method steps, including obtainFreshBeanFactory , prepareBeanFactory , and the invocation of BeanDefinitionRegistryPostProcessor and BeanFactoryPostProcessor implementations, with ordering based on PriorityOrdered and Ordered interfaces.

The registration of BeanPostProcessor instances is described, showing how they are sorted, instantiated, and added to the factory, and how internal post‑processors are re‑registered at the end of the chain.

Bean creation is detailed through finishBeanFactoryInitialization and preInstantiateSingletons , highlighting the checks for singleton, non‑lazy beans and the use of getBean which delegates to createBean and doCreateBean .

Key phases of doCreateBean are covered: instance creation via constructors or factory methods, post‑processing of merged bean definitions, early singleton exposure for circular‑reference handling, property population via populateBean , and initialization with invokeAwareMethods , applyBeanPostProcessorsBeforeInitialization , invokeInitMethods , and applyBeanPostProcessorsAfterInitialization .

The article also discusses how @Autowired and @Resource annotations are processed by AutowiredAnnotationBeanPostProcessor and CommonAnnotationBeanPostProcessor , and how Aware interfaces and @PostConstruct / @PreDestroy callbacks are handled.

BackendJavaSpringdependency injectionSpring Frameworkbean lifecycle
Top Architect
Written by

Top Architect

Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.

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.