How Spring Solves Bean Circular Dependencies with a Three‑Level Cache

This article explains Spring's bean lifecycle, detailing how the framework resolves circular dependencies using a three‑level cache (singletonObjects, earlySingletonObjects, singletonFactories), and why a two‑level cache is insufficient, especially when AOP proxies are involved.

Java High-Performance Architecture
Java High-Performance Architecture
Java High-Performance Architecture
How Spring Solves Bean Circular Dependencies with a Three‑Level Cache

Bean Lifecycle

Understanding the bean lifecycle in Spring is essential to see how circular dependencies are handled. Spring's BeanFactory defines many interfaces that represent different stages of a bean's creation, such as instantiation, property population, and initialization.

The doCreateBean method in AbstractAutowireCapableBeanFactory starts bean creation by instantiating the object via reflection. At this point, dependencies annotated with @Autowired have not yet been injected.

Next, populateBean fills the bean's properties. If a required dependency has not been created, Spring will create it before continuing. Afterwards, initializeBean invokes lifecycle callbacks, including methods from Aware interfaces and any BeanPostProcessor implementations.

During initialization, Spring calls setBeanName, setBeanClassLoader, and setBeanFactory for beans implementing the corresponding Aware interfaces. It also executes postProcessBeforeInitialization and postProcessAfterInitialization of all BeanPostProcessor beans, iterating over each implementation.

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.

aopbackend-developmentcircular-dependencyThree-level Cachebean-lifecycle
Java High-Performance Architecture
Written by

Java High-Performance Architecture

Sharing Java development articles and resources, including SSM architecture and the Spring ecosystem (Spring Boot, Spring Cloud, MyBatis, Dubbo, Docker), Zookeeper, Redis, architecture design, microservices, message queues, Git, etc.

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.