什么是SpringBoot的循环依赖?
在IOC容器中我们会出现在BeanA中引用BeanB,BeanB再调用BeanA。类似于死锁,两个bean都无法创建并注入到对方。
问题代码
@Component
public class BeanA(){
@Autowired
private BeanB beanB;
// other方法
}
@Component
public class BeanB(){
@Autowired
private BeanA beanA;
// other方法
}
解决方案:
一、延迟注入注解
在其中一个被注入的类上贴@Lazy