今天写代码,一顿操作,封装等待。启动发现报错了,翻车现场。。。
我的项目中代码
@Component
public class A {
@Autowired
private B b;
}
@Component
public class B {
@Autowired
private A a;
}
显然,出现了循环依赖。
解决方案
1. 设计层面解决
各业务层面的类做好分层,只有一个方向依赖关系,从根本上消灭循环依赖。
比如 Controller -> Service -> Dao
Service之间不做相互依赖
2. Spring懒加载
@Lazy
@Autowired
private A a;
至此,问题解决。