作者:微信小助手
发布时间:2020-04-23T13:50:23
循环依赖就是N个类中循环嵌套引用,如果在日常开发中我们用new 对象的方式发生这种循环依赖的话程序会在运行时一直循环调用,直至内存溢出报错。
下面说一下Spring是如果解决循环依赖的。
public
class
StudentA {
private StudentB studentB ;
public
void
setStudentB
(StudentB studentB) {
this.studentB = studentB;
}
public
StudentA
() {
}
public
StudentA
(StudentB studentB) {
this.studentB = studentB;
}
}
public
class
StudentB {
private StudentC studentC ;
public
void
setStudentC
(StudentC studentC) {
this.studentC = studentC;
}
public
StudentB
() {
}
public
StudentB
(StudentC studentC) {
this.studentC = studentC;
}
}
public
class
StudentC {
private StudentA studentA ;
public
void
setStudentA
(StudentA studentA) {
this.studentA = studentA;
}
public
StudentC
() {
}
public
StudentC
(StudentA studentA) {
this.studentA = studentA;
}
}
<bean id=
"a"
class=
"com.zfx.student.StudentA">
<
constructor-arg
index=
"0"
ref=
"b">
</
constructor-arg>
</
bean>
<bean id=
"b"
class=
"com.zfx.student.StudentB">
<
constructor-arg
index=
"0"
ref=