Setter DI vs. Constructor DI in Spring?

When it comes to Spring specific pros and cons:

  • Constructor injection (from the definition) does not allow you to create circular dependencies between beans. This limitation is actually an advantage of constructor injection - Spring can resolve circular dependencies when setter injection is used without you even noticing.

  • On the other hand if you use constructor injection CGLIB is not able to create a proxy, forcing you to either use interface-based proxies or a dummy no-arg constructor. See: SPR-3150


You should be deciding based on design considerations, not tool (Spring) considerations. Unfortunately, Spring has trained us to use setter injection because when it was originally conceived, there was no such thing as an "annotation" in Java, and in XML, setter injection works and looks much better. Today, we're freed from those constraints, thus allowing it to be a design decision again. Your beans should use constructor injection for any dependencies that are required by the bean and setter injection for dependencies that are optional and have a reasonable default, more or less as OOD has been telling us from the beginning.


Constructor Injection: We are injecting the dependencies through Constructor.

Generally we can use for Mandatory dependencies.

If you use the Constructor injection there is one disadvantage called "Circular Dependency".

Circular Dependency: Assume A and B. A is dependent on B. B is dependent on A. In this constructor injection will be failed. At that time Setter injection is useful.

If Object state is not inconsistent it won't create Object.

Setter Injection: We are injecting the dependencies through Setter methods.

This is useful for Non-Mandatory dependencies.

It is possible to re injecting dependencies by using Setter Injection. It is not possible in Constructor injection.