Difference between constructor and setter injection
Up to now we came through the concept about setter
injection and constructor injection with dependencies right, now
let us see the difference between setter and constructor
- Partial dependency: can be injected using setter
injection but it is not possible by constructor. Suppose there
are 3 properties in a class, having 3 arg constructor and
setters methods. In such case, if you want to pass information
for only one property, it is possible by setter method only.
- Overriding: Setter injection overrides the
constructor injection. If we use both constructor and setter
injection, IOC container will use the setter injection.
- Changes: We can easily change the value by setter
injection. It doesn't create a new bean instance always like
constructor. So setter injection is flexible than constructor
In this post, you will learn: