构造函数的引入会造成循环依赖
import org.springframework.stereotype.Component;/*** @author tn* @version 1* @ClassName Sa* @description 测试A* @date 2020/11/13 11:23*/@Componentpublic class Sa {private Sb sb;public Sa(Sb sb) {this.sb = sb;}}import org.springframework.stereotype.Component;/*** @author tn* @version 1* @ClassName Sb* @description 测试B* @date 2020/11/13 11:23*/@Componentpublic class Sb {private Sc sc;public Sb(Sc sc) {this.sc = sc;}}import org.springframework.stereotype.Component;/*** @author tn* @version 1* @ClassName Sc* @description 测试C* @date 2020/11/13 11:23*/@Componentpublic class Sc {private Sa sa;public Sc(Sa sa) {this.sa = sa;}}Description:The dependencies of some of the beans in the application context form a cycle:┌─────┐| sa defined in file [D:\project\java\my\test\stest\loop_1\target\classes\com\tn\loop_1\test\Sa.class]↑ ↓| sb defined in file [D:\project\java\my\test\stest\loop_1\target\classes\com\tn\loop_1\test\Sb.class]↑ ↓| sc defined in file [D:\project\java\my\test\stest\loop_1\target\classes\com\tn\loop_1\test\Sc.class]└─────┘Process finished with exit code 1
使用 注解 就不会出现循环依赖,但是使用时可能会报错(下面的例子,会疯狂的互相调用)
package com.tn.loop_1.test;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;/*** @author tn* @version 1* @ClassName Sa* @description 测试A* @date 2020/11/13 11:23*/@Componentpublic class Sa1 {@Autowiredprivate Sb1 sb1;public void print(){System.out.println("测试A");sb1.print();}}package com.tn.loop_1.test;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;/*** @author tn* @version 1* @ClassName Sb* @description 测试B* @date 2020/11/13 11:23*/@Componentpublic class Sb1 {@Autowiredprivate Sc1 sc1;public void print(){System.out.println("测试B");sc1.print();}}package com.tn.loop_1.test;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;/*** @author tn* @version 1* @ClassName Sc* @description 测试C* @date 2020/11/13 11:23*/@Componentpublic class Sc1 {@Autowiredprivate Sa1 sa1;public void print(){System.out.println("测试C");sa1.print();}}
