Mastering Spring Bean Creation: From Simple Instantiation to 3‑Level Caching

This article explains Spring's bean lifecycle in depth, covering the minimal creation steps, the role of BeanFactoryPostProcessor and BeanPostProcessor, how Spring resolves references and circular dependencies, and why a three‑level cache is essential for reliable dependency injection.

Architect's Guide
Architect's Guide
Architect's Guide
Mastering Spring Bean Creation: From Simple Instantiation to 3‑Level Caching

1 Introduction

Spring is a core framework for Java backend development and a frequent interview topic. Common questions include when proxy objects are created, how circular dependencies are resolved, and how the three‑level cache works.

2 Minimal Bean Creation Process

Obtain Bean Definition Scan the project for annotated beans, collect type, name, properties, constructors, and store them in a map.

Instantiate Bean Iterate the map and create an instance using the no‑arg constructor, similar to new .

Populate Properties After instantiation, default‑valued fields are filled with the required data.

Initialize Bean If the bean implements InitializingBean , Spring calls its afterPropertiesSet method.

These four steps constitute the core bean creation logic. Spring also provides extension points before and after steps 2‑4 via BeanPostProcessor implementations.

3 Post‑Processors

Spring defines two main post‑processor types: BeanFactoryPostProcessor (including BeanDefinitionRegistryPostProcessor) – operates on the bean factory before any bean instances are created. BeanPostProcessor – operates on bean instances during their creation.

BeanFactoryPostProcessor

Used to modify bean definitions or add custom beans. For example, MyBatis registers a MapperFactoryBean for interfaces annotated with @Mapper via a factory post‑processor.

The post‑processor creates a bean definition for the mapper and registers it. When the bean is needed, MyBatis calls getObject() on the factory to obtain a proxy instance and places it in the container.

BeanPostProcessor

Executed at specific points of bean creation; a diagram (omitted) illustrates the detailed lifecycle.

Resolving the Proxy‑Target‑Class Issue

The problem described earlier involves the internal bean org.springframework.aop.config.internalAutoProxyCreator whose proxy-target-class property determines whether JDK dynamic proxies or CGLIB are used. A third‑party library set this property to true, breaking a project that relied on JDK proxies. By implementing a custom BeanFactoryPostProcessor, the property can be forced to false after other libraries have set it.

4 References and Caches

When many beans exist, Spring uses caches to manage references.

Reference Existing Bean

The parent bean can directly retrieve the child bean from the first‑level cache.

Reference Uncreated Bean

If a parent bean is created before its child, Spring temporarily stores the partially created parent in a third‑level cache (the “early bean reference” cache) while it creates the child.

The third‑level cache holds bean factories; getEarlyBeanReference retrieves the unfinished bean.

Circular Dependency

When two beans depend on each other, Spring records each bean name in a SingletonCurrentlyInCreation set. Detecting a repeat entry signals a circular dependency.

Spring then retrieves the partially created bean from the third‑level cache, applies any necessary proxying via wrapIfNecessary, and moves it to the second‑level cache.

Proxy in Circular Dependency

Even during early exposure, the same proxy creation method ( wrapIfNecessary) is used, ensuring that proxy behavior is consistent whether the bean is fully or partially created.

5 Decoding the Three‑Level Cache

The three caches correspond to different bean states: first‑level holds fully initialized beans, second‑level holds early exposed beans (often proxies), and third‑level holds bean factories for beans that are still being created.

Although the cache layers are logical rather than performance‑driven, they simplify handling of circular dependencies and proxy creation.

6 Detailed Bean Creation Flow (Spring 4)

The following diagram (the most detailed publicly available) shows the complete sequence of steps Spring performs when creating a bean.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Backend Developmentspringdependency-injectionThree-level Cachebean-lifecyclePostProcessor
Architect's Guide
Written by

Architect's Guide

Dedicated to sharing programmer-architect skills—Java backend, system, microservice, and distributed architectures—to help you become a senior architect.

0 followers
Reader feedback

How this landed with the community

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.