I have a big project with SpringBoot, I have a lot of @Service
and, inside these, I have other @Autowired
service as dependencies.
Now, to reduce code verbosity, I have annotated every service class with @RequiredArgsConstructor(onConstructor = @__(@Autowired))
and put all nested service private. For Example:
@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
Now wen launching my Tomcat (for test) server I obtain the follow error:
The dependencies of some of the beans in the application context form a cycle:
...
... // some inputated classes
How can I solve this?
----------------------- UPDATE ------------------
Predominantly the question is: why I can do this:
@Service
class A {
@Autowired
private B b;
}
@Service
class B {
@Autowired
private A a;
}
And cannot do this:
@Service
@RequiredArgsConstructor(onConstructor_={@Autowired})
class A {
private final B b;
}
@Service
@RequiredArgsConstructor(onConstructor_={@Autowired})
class B {
private final A a;
}
Thanks.