SpringBoot循环依赖问题处理

58 阅读1分钟

1.描述

AService注入BService,同时BService注入AService。即造成的局面是相互依赖。

@Service  
public class AServiceImpl implements AService {  
    @Autowired  
    private BService bService;  
}

@Service
public class BServiceImpl implements BService {
    @Autowired
    private AService aService;
}

2.解决

方式一、配置文件添加配置,配置了true之后即允许循环依赖,Spring会使用三级缓存去处理循环依赖问题

spring:
  main:
    allow-circular-references: true

方式二、添加注解@Lazy

@Service
public class AServiceImpl implements AService {
    @Autowired
    @Lazy
    private BService bService;
}

引用:https://blog.csdn.net/m0_73799407/article/details/144663321