Scrivio Interview library
InternalsIntermediate6 min read · updated 12 November 2026

How does the Spring bean factory resolve circular dependencies between field-injected singletons when they are allowed, and why can this break with proxies?

When circular references are allowed, Spring creates the first bean with its constructor and, before injecting its fields, registers an ObjectFactory for it in a third-level cache called singletonFactories. When the second bean asks for the first, Spring calls that factory, receives an early reference, and moves it into the earlySingletonObjects cache. The factory goes through SmartInstantiationAwareBeanPostProcessor.getEarlyBeanReference, so the AOP proxy creator can hand out a proxy early; if another post-processor later wraps the bean differently, the reference already injected no longer matches the final bean and Spring fails. Constructor cycles cannot use any of this, because no instance exists to expose.

Why this question is asked

Most of us know that Spring 'can resolve circular dependencies', but few of us can say how. Interviewers ask this at the ten-year level because the mechanism explains several confusing production errors: a bean that holds a raw object instead of its proxy, and the message that a bean 'has been injected into other beans in its raw version'. In this answer, we will recap the three singleton caches, follow the creation of two field-injected beans step by step, see how the proxy creator joins in through getEarlyBeanReference, reproduce the case where it breaks, and explain why constructor cycles are never resolved.

1A Quick Recap: Creating a Bean in Three Phases

Inside DefaultListableBeanFactory, creating a singleton has three phases. First instantiation: Spring calls the constructor and gets a raw object whose fields are still empty. Second, population: Spring fills @Autowired fields and calls setters. Third, initialisation: Aware callbacks, @PostConstruct, afterPropertiesSet and the BeanPostProcessors run, and this is where an AOP proxy, a generated wrapper that adds transactions or async behaviour, usually replaces the raw object.

A cycle can only be resolved in the gap between phase one and phase two. At that moment the object exists, so another bean could hold a reference to it, but it is not finished. The whole mechanism is about handing out that unfinished object safely.

The three singleton caches in DefaultSingletonBeanRegistry
CacheHoldsFilled when
singletonObjectsfinished beansthe bean has completed all three phases
earlySingletonObjectsearly references already handed outanother bean asked for a bean still in creation
singletonFactoriesan ObjectFactory per bean in creationright after the constructor, before population
Lookups go from top to bottom; a hit in the factory cache is moved to the early cache.

2Following Two Field-Injected Beans Step by Step

Take two services that inject each other through fields, in an application that sets spring.main.allow-circular-references=true (Spring Boot forbids this by default since 2.6; plain Spring Framework allows it).

Java
@Service
class A { @Autowired B b; }
@Service
class B { @Autowired A a; }
// Simplified from AbstractAutowireCapableBeanFactory.doCreateBean:
Object bean = createBeanInstance("a"); // raw A, field b is null
addSingletonFactory("a", () -> getEarlyBeanReference("a", mbd, bean)); // third-level cache
populateBean("a"); // needs B, so B is created now
Object exposed = initializeBean("a", bean); // post-processors may wrap A here
Step throughResolving A -> B -> A with field injection
Bean factoryABCaches
new A()
register ObjectFactory for a
populate A: needs B, new B()
get a: factory found, early reference
early A, moved to early cache
inject early A into B, finish B
inject B into A, initialize A
a and b into singletonObjects

8 messages in order. Play them, or step through them one at a time.

Spring starts with A. It calls the constructor, gets a raw A, and immediately registers an ObjectFactory for it in singletonFactories. Notice that it registers a factory, a small lambda, rather than the object itself. The reason is that Spring does not yet know whether anybody will need A early, and computing an early reference may be expensive because it may create a proxy.

Now Spring populates A and finds the field b. B does not exist, so Spring creates B the same way: constructor, factory registered, population. B needs A. Spring calls getSingleton("a"), misses in singletonObjects, misses in earlySingletonObjects, finds the factory in singletonFactories, calls it, and gets the early reference. It stores that reference in earlySingletonObjects and removes the factory, so every later request for A receives the same object. B is then finished and goes into singletonObjects, and A gets its field b and finishes too.

3How the Proxy Creator Joins In

The factory does not just return the raw A. It calls getEarlyBeanReference on every SmartInstantiationAwareBeanPostProcessor, a post-processor interface with a hook for exactly this moment. The AOP auto-proxy creator (AbstractAutoProxyCreator, used for @Transactional, @Cacheable and aspects) implements it. If A needs a proxy, it creates the proxy now, gives it to B, and remembers that it has already proxied A.

Later, when A reaches postProcessAfterInitialization, the same proxy creator sees that it already handled A early and returns A unchanged. Back in doCreateBean, Spring then compares: the object after initialisation is the raw A, and an early reference exists, so it registers the early reference, the proxy, as the final bean. B and the context now hold the same proxy, and everything is consistent.

4When It Breaks: A Post-Processor That Wraps Late

The trouble starts when a post-processor wraps the bean in postProcessAfterInitialization without supporting getEarlyBeanReference. A custom BeanPostProcessor that returns its own proxy is one example. A well-known built-in one is @Async: it is handled by AsyncAnnotationBeanPostProcessor, a separate post-processor that does not take part in early references, so an @Async bean in a cycle is a common way to meet this error.

Java
@Component
class AuditWrapper implements BeanPostProcessor {
public Object postProcessAfterInitialization(Object bean, String name) {
return name.equals("a") ? wrapWithAuditProxy(bean) : bean; // A is replaced here, too late
}
}
// Startup fails:
// BeanCurrentlyInCreationException: Error creating bean with name 'a': Bean with name 'a' has been
// injected into other beans [b] in its raw version as part of a circular reference, but has eventually
// been wrapped. This means that said other beans do not use the final version of the bean.

Notice what happened. B received the early reference, the raw A, during the cycle. After initialisation the final A is an audit proxy, a different object. Spring checks exactly this case: the exposed object differs from the raw bean, and other beans already hold the early reference. Rather than start an application where B silently bypasses auditing or transactions, it fails. So the error is a safety check, not a bug in Spring.

5Why Constructor Cycles Cannot Use This Mechanism

Everything above depends on the gap between instantiation and population. With constructor injection there is no gap: the dependencies are the constructor's arguments, so Spring must have a finished B before it can create any A at all. When B's constructor then asks for A, there is no raw A and no factory in singletonFactories, only a note that A is currently in creation. Spring throws BeanCurrentlyInCreationException.

This is one reason constructor injection is recommended. It makes cycles fail fast and loudly, instead of being resolved by a mechanism that only works until the first post-processor that wraps late.

6Follow-up questions

Why does Spring need a third cache instead of putting the raw object straight into earlySingletonObjects?

Because the early reference may need to be a proxy, and creating it is work that should only happen if somebody asks. The factory in singletonFactories defers that decision. Most beans are never requested early, so their factories are simply discarded when they finish, and no early proxy is ever created.

Can @Lazy make a constructor cycle work?

Yes, because @Lazy on one constructor parameter makes Spring inject a lazy-resolution proxy instead of the real bean. The proxy can be created without the other bean existing, so the constructor can be called. It removes the startup failure but not the design problem, and it hides errors until the first method call.

What does allowRawInjectionDespiteWrapping do?

It is a flag on AbstractAutowireCapableBeanFactory that turns the final consistency check off, so Spring accepts that other beans hold the raw version. It is almost never a good idea, because those beans will bypass the wrapper, for example transactions, without any warning.

7Conclusion

In this answer, we saw that Spring resolves field and setter cycles by registering an ObjectFactory in singletonFactories right after the constructor runs, handing out an early reference when another bean asks, and caching it in earlySingletonObjects. The factory calls getEarlyBeanReference, which lets the AOP proxy creator supply its proxy early and keep the bean consistent. A post-processor that wraps the bean only after initialisation breaks that consistency, and Spring fails rather than run with a raw reference. Constructor cycles never reach this mechanism, because no instance exists to expose.

What to say in the interviewAbout 70 seconds aloud

Open with

Spring keeps three caches for singletons. When it creates a bean, it calls the constructor and, before injecting any fields, registers an ObjectFactory for that raw object in the singletonFactories cache.

Then, if they want more

If, while populating the first bean, it creates a second bean that needs the first, the lookup misses the finished and early caches, finds the factory, calls it, and moves the result into earlySingletonObjects, so the second bean gets an early reference and can finish.

The factory calls getEarlyBeanReference on SmartInstantiationAwareBeanPostProcessors, which is how the auto-proxy creator can hand out the transactional proxy early and then skip proxying again later, so both beans see the same object.

It breaks when another post-processor wraps the bean in postProcessAfterInitialization without that early hook: the bean already injected the raw version, the final bean is a different object, and Spring fails with the 'injected into other beans in its raw version' error instead of running inconsistently.

Constructor cycles cannot use this at all, because there is no instance to expose until all constructor arguments exist. That is also why I prefer constructor injection and fix cycles in the design.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "How does the Spring bean factory resolve circular dependencies between field-injected...": https://scrivio-interview-library.netlify.app/spring-boot/dependency-injection/3165b0a69d/