I've noticed a bit of a weird bug in an application I'm working on. While modifying a class, I moved a bunch of properties to be autowired in the constructor instead of using field injection, however, this caused me to get an error while starting up because of a circular dependency. Here's the breakdown of the dependency that caused an error:
- I am trying to use
ServiceAinside ofTargetClass. ServiceAhasServiceBinjected through its constructorServiceBhasServiceCinjected through field injectionServiceChasTargetClassinjected through field injection
I'm looking into ways of refactoring this, or trying to move some logic around into better places so I remove this circular dependency, however, I'm curious as to why two different methods of injecting ServiceA cause two different outcomes:
If I inject ServiceA through field injection, just using @Autowired on the field, my app starts just fine. However, if I switch TargetClass to use constructor injection, I will get an error about a circular dependency.
What is the difference in the way Spring handles the two types of injection that causes one to fail in this scenario, and the other to work?