使用自动装配功能时Spring中的循环依赖项错误

时间:2018-10-18 22:05:00

标签: spring spring-boot ioc-container

我正在使用带有以下代码的Spring Boot版本1.5.10.RELEASE(最新版本之一):

@Service
public class AService {

private AService aService; //Self autowire

public AService(AService aService){
    this.aService = aService;
}

@Cacheable(value = "aCacheName")
public List<SomeClass> expensiveOperation(){
    //Very expensive operation that can be cached
}

public List<SomeClass> otherOperation(){
    return aService.expensiveOperation().otherOperation()); //Call proxy, can't use this.expensiveOperation() because it will bypass the cache
}
}

我收到此错误:

“应用程序上下文中某些bean的依赖性形成一个循环。”

我知道Spring允许“自动装配”,我在做什么错了?

谢谢。

1 个答案:

答案 0 :(得分:1)

this有什么问题?您也可以将其标记为@Lazy,并使用字段autowire或构造函数的设置向导。您将无法使用构造函数IMHO做到这一点,因为您永远无法提供构造实例。