有三个类:
@Service public class AService { @Autowired private BService bService; } @Service public class BService { @Autowired private CService cService; } @Service public class CService { @Autowired private AService aService; }
造成循环依赖报错:
The dependencies of some of the beans in the application context form a cycle: ┌─────┐ | AService (field private com.example.demo.service.BService com.example.demo.service.AService.bService) ↑ ↓ | BService (field private com.example.demo.service.CService com.example.demo.service.BService.cService) ↑ ↓ | CService (field private com.example.demo.service.AService com.example.demo.service.CService.aService) └─────┘解决方案
对其中一个进行改造,例如对CService改造,采用如下注入方式:
@Service public class CService { private AService aService; // 去掉@Autowired // 采用构造器方式,加入Lazy注解 public CService(@Lazy AService aService) { this.aService = aService; } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)