循环依赖指的是两个或多个 Bean 之间相互持有对方的依赖,形成依赖环。
- 构造器循环依赖:
Error creating bean with name 'constructorA': Requested bean is currently in creation: Is there an unresolvable circular reference? ConstructorTest.java
- Setter 循环依赖(支持):
- 字段注入循环依赖:
StackOverflowError异常,本质上是一个递归调用的过程,当调用到一定深度的时候,就会抛出异常。 FieldTest.java
- 多例 Bean 循环依赖:
Requested bean is currently in creation: Is there an unresolvable circular reference PrototypeTest.java
- 代理对象循环依赖
Exception in thread "main" org.springframework.beans.factory.BeanCurrentlyInCreationException:
Error creating bean with name 'proxyA': Bean with name 'proxyA'
has been injected into other beans [proxyB] 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. This is often the result of over-eager type matching -
consider using 'getBeanNamesForType' with the 'allowEagerInit' flag turned off, for example.