Newsletter |
Difference between Setter Injection and Constructor Injection
Spring » on Aug 17, 2011 { 24 Comments } By Sivateja
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 injection…
Setter Injection | Constructor Injection | |
---|---|---|
1. 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 | 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 | |
2. Setter Injection will overrides the constructor injection value, provided if we write setter and constructor injection for the same property [i already told regarding this, hope you remember ] | 2. But, constructor injection cannot overrides the setter injected values | |
3. 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. | 3. In this case, Constructor injection is highly recommended, as we can inject all the dependencies with in 3 to 4 lines [i mean, by calling one constructor] | |
4. Setter injection makes bean class object as mutable [We can change ] | 4. Constructor injection makes bean class object as immutable [We cannot change ] |
Actually we may have some more differences, but these are what i found upto my knowledge π
β ββ
You Might Also Like
::. About the Author .:: | ||
Comments
24 Responses to “Difference between Setter Injection and Constructor Injection”
Great post, I think people should learn a lot from this weblog its real user friendly. So much good information on here :D.
” Setter injection makes bean class object as mutable [We can change ]”
please provide explanation for it
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.
@sankar
Hey am really sorry, i just overlooked your comment.
Actually with setter injection we can assign the values again, but constructor injection will be executed only once, some object creation time.
Some how we can do something with setter injection even after creation of the object. But its not the case in constructor injection.
Hope you got it.
Hi,
one more difference is through constructor we can assigned the value to final field variable ,but with setter we can’t.
I always choose setter one because of readability. This link also has some good points
Hi,
please you provide Spring MVC3.1
The Setter injection uses tag to inject bean values .
The Constructor Injection uses tag to inject bean values .
I think short but sweet…
Great post. I appreciate the tremendous effort made in this blog. This is a big contribution. If there is anything I would like to suggest, it would be language improvement.
Hi,
Two more difference are:
1)Through constructor we can assigned the value to final field variable ,but with setter we canβt.
2)Through setter injection, we can over come circular dependency. But with constructor we can’t.
Nice explanation.
Lessons are too good except your english.
Can we specify data type, when we are passing value in setter injection?
GOOD EXPLANATION FOR EACH TOPIC.THANK U!
nice explanation from java4 guys
What is the meaning of this please provide the explanation of in real thing example:
Setter Injection will overrides the constructor injection value, provided if we write setter and constructor injection for the same property
Nice dude….
Please provide Explanation for the below differences.
1)Through constructor we can assigned the value to final field variable ,but with setter we canβt.
2)Through setter injection, we can over come circular dependency. But with constructor we canβt.
Hi Yuvashree:
Please look into the explanation below for both the points :
1) When you use constructor injection, the parameters you give will be assigned to that object while creating. Now if you wish to change them, you can't do that as you will be creating the new object by calling the constructor injection. But for setter injection, it is not necessary that you provide all the information i.e. dependencies while creating the object. If you call a method setXXX() initially doesn't mean you can not call it again, which means you are just calling the same method for the same instance and hence changing the field values for the same object.
2) Circular dependency means two objects mutually dependent on each other to create the object. Below is the example I have found will make you understand it better :
<bean id="A" class="my.company.A">
<property name="dependency" ref="B"/>
</bean>
<bean id="B" class="my.company.B">
<property name="dependency" ref="A"/>
</bean>
Here both the objects are waiting till the other object is created.
But in case of setter injection, the object is already created and we just have to pass on the dependencies by calling the setter methods.
Hope this helps. Correct me if any mistakes found.
Short and Sweet explanation!
@Neelakanta Reddy :
This is really very good point.
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.
Could you please explain the line 4 in setter and constructor injection
Nice explanation everyone can understand..