top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What is the difference between constructor injection and setter injection?

0 votes
315 views
What is the difference between constructor injection and setter injection?
posted Jun 10, 2016 by Karthick.c

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

1.In constructor injection, partial injection of dependencies cannot possible, because for calling constructor we must pass all the arguments right, if not so we may get error but In Setter Injection, partial injection of dependencies can possible, means if we have 3 dependencies like int, string, long, then its not necessary to inject all values if we use setter injection. If you are not inject it will takes default values for those primitives.
2.Constructor injection cannot overrides the setter injected values whereas Setter Injection will overrides the constructor injection value, provided if we write setter and constructor injection for the same property.
3.Constructor injection is highly recommended, as we can inject all the dependencies by calling one constructor and If we have more dependencies for example 15 to 20 are there in our bean class then, in this case setter injection is not recommended as we need to write almost 20 setters right, bean length will increase.
4.Constructor injection makes bean class object as immutable and Setter injection makes bean class object as mutable means we can change it.

answer Jun 11, 2016 by Shivam Kumar Pandey
...