带有循环引用的Spring Boot和依赖注入

时间:2019-07-11 14:04:03

标签: java spring-boot

我有一个非常不幸的情况,看起来像这样:

 // TestController
 @Inject
 public TestController(ServiceOne serviceOne){
      //
 }

 // ServiceOne
 @Inject
 public ServiceOne(ServiceTwo serviceTwo){
      //
 }

 // ServiceTwo
 @Inject
 public ServiceTwo(ServiceOne serviceOne){
      //
 }

我得到:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name testController

nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException

很明显,这里我有一个循环依赖关系,这是错误的根源,但是我不确定当我们有两个相互依赖的类(例如ServiceOne和ServiceTwo)时,正确的解决方案是什么?

0 个答案:

没有答案