Spring对循环依赖的支持情况
主要分为两种方式,每种方式又有 scope为singleton和prototype
-
构造函数方式注入(singleton或prototype)
-
Set方式注入(singleton或prototype)
对于scope=prototype的Bean,以上两种注入方式,都不支持循环依赖:如下所示:
@Repository
@Scope(value = "prototype")
public class Company {
private Staff staff;
@Autowired
public Company(Staff staff) {
this.staff = staff;
}
}
@Repository
@Scope(value = "prototype")
public class Staff {
private Company company;
@Autowired
public Staff(Company company) {
this.company = company;
}
}
/**
* 注解引入
*
* @param args
*/
public static void main(String args[]) {
System.out.println("Spring 项目成功编译!!!!走注解使用Spring");
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(Entrance.class);
Company company = (Company) applicationContext.getBean("company");
}
输出如下:
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'staff' defined in file [/Users/wsq/Documents/spring/spring-framework-5.2.0.RELEASE/springdemo/build/classes/java/main/cn/com/yuns/dao/impl/Staff.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'company': Requested bean is currently in creation: Is there an unresolvable circular reference?
@Repository
@Scope(value = "prototype")
public class BoyFriend {
@Autowired
private GirlFriend girlFriend;
}
@Repository
@Scope(value = "prototype")
public class GirlFriend {
@Autowired
private BoyFriend boyFriend;
}
/**
* 注解引入
*
* @param args
*/
public static void main(String args[]) {
System.out.println("Spring 项目成功编译!!!!走注解使用Spring");
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(Entrance.class);
GirlFriend girlFriend = (GirlFriend) applicationContext.getBean("girlFriend");
}
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'boyFriend': Unsatisfied dependency expressed through field 'girlFriend'; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'girlFriend': Requested bean is currently in creation: Is there an unresolvable circular reference?
Spring对于构造函数方式注入的依赖,不支持循环依赖
如下所示:
@Repository
public class Company {
private Staff staff;
@Autowired
public Company(Staff staff) {
this.staff = staff;
}
}
@Repository
public class Staff {
private Company company;
@Autowired
public Staff(Company company) {
this.company = company;
}
}
/**
* 注解引入
*
* @param args
*/
public static void main(String args[]) {
System.out.println("Spring 项目成功编译!!!!走注解使用Spring");
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(Entrance.class);
Company company = (Company) applicationContext.getBean("company");
}
输出如下:
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'staff' defined in file [/Users/wsq/Documents/spring/spring-framework-5.2.0.RELEASE/springdemo/build/classes/java/main/cn/com/yuns/dao/impl/Staff.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'company': Requested bean is currently in creation: Is there an unresolvable circular reference?