Constructor injection on spring configuration classes

642 Views Asked by At

In several code examples I see autowiring used instead of constructor injection. Can any one explain why Constructor injection is difficult but autowiring is possible on spring configuration classes? Thanks in advance.

this question does touch the topic but doesn't have the detailed answer.

1

There are 1 best solutions below

0
On

Constructor injection requires all dependencies are available beforehand.

In early Spring Boot (before 2.6) circular dependencies are allowed by default. It is impossible to have circular bean dependencies together with the constructor injection (egg-chicken problem if A => B & B => A and both use it).

I think for this reason such expression of DI dependencies was implemented later. With a default constructor + setter or @Autowired you could resolve circular dependencies.