使用自自动布线功能时,Spring中出现循环依赖项错误

使用自自动布线功能时,Spring中出现循环依赖项错误,spring,spring-boot,ioc-container,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<SomeClas

我使用的是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
}
}
@服务
公共类服务{
专用AService AService;//自动连线
公共AService(AService AService){
this.aService=aService;
}
@可缓存(value=“aCacheName”)
公开列表费用操作(){
//非常昂贵的操作,可以缓存
}
公共列表操作(){
返回aService.expensiveOperation().otherOperation());//调用代理,无法使用此.expensiveOperation(),因为它将绕过缓存
}
}
我得到了这个错误:

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

我知道Spring允许“自动布线”,我做错了什么


谢谢。

这个
有什么问题吗??您还可以将其标记为@Lazy,并使用字段autowire或ever setter代替构造函数。您将无法使用构造函数IMHO执行此操作,因为您将永远无法为构造提供实例。

如果您调用“this”,它将再次运行expensiveOperation(),即@Cacheable下的操作。您始终可以注入应用程序上下文并获取bean。您甚至可以在@PostConstruct中进行赋值,因为此时所有bean都应该是可注入的。