Backend Development 14 min read

Understanding Spring Bean Instantiation Process

This article explains how Spring's IoC container creates and manages beans, covering the container startup phase, bean definition loading, BeanFactoryPostProcessor and BeanPostProcessor extensions, object creation strategies, property injection, Aware interfaces, and bean lifecycle callbacks.

Top Architect
Top Architect
Top Architect
Understanding Spring Bean Instantiation Process

Spring's IoC container works like a production line: it first prepares configuration metadata, then creates and manages bean instances. The whole bean creation can be divided into two major phases: the container startup phase and the bean instantiation phase.

Container Startup Phase

During this phase Spring loads configuration metadata from XML, properties files, annotations, or code, and represents each bean definition as a BeanDefinition . Different BeanDefinitionReader implementations (e.g., XmlBeanDefinitionReader , PropertiesBeanDefinitionReader , AnnotatedBeanDefinitionReader ) parse the source and register the resulting BeanDefinition objects in a BeanDefinitionRegistry .

Before any bean is instantiated, BeanFactoryPostProcessor can modify the registered definitions, for example replacing placeholder values:

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
  <property name="url" value="${jdbc.url}"/>
  <property name="username" value="${jdbc.username}"/>
  <property name="password" value="${jdbc.password}"/>
</bean>

Bean Instantiation Phase

When an application requests a bean, Spring creates the object using a strategy pattern: either simple reflection or a CGLIB proxy. The created instance is wrapped in a BeanWrapper , which simplifies reflective property access.

After creation, Spring injects properties. Primitive values are set directly from the definition; reference properties are resolved from already‑created beans, and if a dependency is missing Spring temporarily pauses the current bean, creates the required dependency, then resumes.

Spring also checks for Aware interfaces (e.g., ApplicationContextAware , ResourceLoaderAware ) and injects the corresponding container objects.

BeanPostProcessor Extensions

During the bean lifecycle, BeanPostProcessor runs both before and after custom initialization. It enables features such as AOP proxy creation and allows developers to modify or replace bean instances.

Custom initialization can be defined by implementing InitializingBean or specifying an init-method . Destruction logic can be provided via DisposableBean or a destroy-method (e.g., closing a datasource).

Summary of Execution Order

The overall order is: load metadata → register BeanDefinition → apply BeanFactoryPostProcessor → create bean instance → wrap with BeanWrapper → inject properties → invoke Aware callbacks → run BeanPostProcessor (pre‑ and post‑) → execute init methods → bean ready for use. When the container shuts down, destroy callbacks are invoked.

BackendJavaIoCSpringdependency injectionBean
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.