Spring对循环依赖的支持情况

2020-07-05  本文已影响0人  码而优则仕

Spring对循环依赖的支持情况

主要分为两种方式,每种方式又有 scope为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?

上一篇 下一篇

猜你喜欢

热点阅读