Partial dependency

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.

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.

Overriding

Setter Injection will overrides the constructor injection value, provided if we write setter and constructor injection for the same property.

But, constructor injection cannot overrides the setter injected values.

Number of dependencies

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.

In this case, Constructor injection is highly recommended, as we can inject all the dependencies with in 3 to 4 lines by calling one constructor.

Changes

Setter injection makes bean class object as mutable. (We can easily change the value by setter injection)

Constructor injection makes bean class object as immutable. (We cannot change the value by Constructor injection)

It's good to share...Share on FacebookTweet about this on TwitterShare on LinkedInPin on PinterestShare on Google+Email this to someone

Leave a Reply

Your email address will not be published. Required fields are marked *