W3Schools Learner's Blog

W3Schools Programming knowledge summary website

div

12/13/2017

the difference between constructor and setter injection

There are many differences between constructor injection and setter injection.
  1. 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.
  2. Overriding: Setter injection overrides the constructor injection. If we use both constructor and setter injection, IOC container will use the setter injection.
  3. 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 injection.

No comments:

Post a Comment

Note: only a member of this blog may post a comment.