Spring boot SpringBoot@RequiredArgsConstructor(onConstructor=@_u(@Autowired))循环依赖项错误

Spring boot SpringBoot@RequiredArgsConstructor(onConstructor=@_u(@Autowired))循环依赖项错误,spring-boot,inversion-of-control,lombok,Spring Boot,Inversion Of Control,Lombok,我有一个关于SpringBoot的大项目,我有很多@服务,在这些服务中,我有其他@自动连线服务作为依赖项 现在,为了减少代码的冗长性,我已经用@RequiredArgsConstructor(onConstructor=@\uuU(@Autowired))注释了每个服务类,并将所有嵌套的服务放在私有位置。例如: @Service @RequiredArgsConstructor(onConstructor = @__(@Autowired)) public class DomandaDocume

我有一个关于SpringBoot的大项目,我有很多
@服务
,在这些服务中,我有其他
@自动连线
服务作为依赖项

现在,为了减少代码的冗长性,我已经用
@RequiredArgsConstructor(onConstructor=@\uuU(@Autowired))
注释了每个服务类,并将所有嵌套的服务放在私有位置。例如:

@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class DomandaDocumentoService {

private final DomandaDocumentoRepository domandaDocumentoRepository;

private final DomandaDatoBeneficiarioService domandaDatoBeneficiarioService;
private final DomandaDatiGeneraliService domandaDatiGeneraliService;
private final DomandaOperazioneService domandaOperazioneService;
private final ParametroValoreService parametroValoreService;
private final SecurityService securityService;

private final DomandaDocumentoMapper domandaDocumentoMapper;

...
...  // other method
现在启动Tomcat(用于测试)服务器时,我得到以下错误:

The dependencies of some of the beans in the application context form a cycle:
...
... // some inputated classes
我怎样才能解决这个问题

-----------------------更新------------------

主要的问题是:为什么我可以这样做:

@Service
class A {
  @Autowired
  private B b;
}

@Service
class B {
  @Autowired
  private A a;
}
而且不能这样做:

@Service
@RequiredArgsConstructor(onConstructor_={@Autowired})
class A {
  private final B b;
}

@Service
@RequiredArgsConstructor(onConstructor_={@Autowired})
class B {
  private final A a;
}

谢谢。

这个问题与lombok没有直接联系,而是为什么在使用构造函数注入时循环依赖会有问题,为什么字段注入不会有问题,或者换句话说,为什么可以这样做:

@服务
公共A类{
@自动连线
私人B,;
}
@服务
公共B级{
@自动连线
私人A;
}
而不是这个

@服务
公共A类{
私人B,;
公共服务A(B){
这个.b=b;
}
}
@服务
公共B级{
私人A;
公共图书馆B(A){
这个a=a;
}
}
发件人:

…bean a和bean B之间的循环依赖关系强制 在完全初始化之前注入另一个bean 本身(一个经典的鸡和蛋的场景)…一个可能的解决方案是 编辑要由setter配置的某些类的源代码 而不是构造函数

因此,为了解决您的问题,Spring文档建议您对导致循环依赖的bean使用setter(或field)注入。你不必为双方都这么做,像这样的事情应该足够了:

@服务
公共A类{
@自动连线
私人B,;
}
@服务
@RequiredArgsConstructor(onConstructor_={@Autowired})
公共B级{
私人决赛A;
}
我认为不会有那么多循环依赖bean,所以我只为错误日志中看到的那些bean设置字段注入(同样,不是所有的,只是一些,以打破构造函数注入bean的循环)

作为旁注,当bean形成一个循环时,在代码中,它可能应该被重构