Difference between Setter injection & constructor injection?
è
If bean class contains only one property or if
all the property of bean class should participate in DI then go for constructor
injection ,because construct injection is very faster than setter injection
è
If bean class contains more than one property
and there is no need of making all properties participating in DI then we need
to go for setter injection.
Eg:-
If bean class contains four properties and there is no need
of making all properties participating in DI then we need to write max of four
setter methods to performs setter injection. But we need to write four
factorial (24) constructors to supports to constructor injection in all angles.
Settter Injection
|
Constructor
Injection
|
|
1. Performs injection after creating mean class object so it is
bit delay injection
|
1. Performs injection
while creating bean class oj so no delay in injection
|
|
2.
Support cyclic dependency injection
|
2. Doesn’t supports cyclic injection
|
|
3.
<property> tag is required.
|
3. <condtructor-args> tag is required
|
|
4.
To preforms setter injection on “n- parameters” in all angles
“n-setter” methods are required.
|
4. To preforms
constructor injection on “n-parameters” in all angles n! Constructors are
required.
|
|
5.
If all properties are configured for setter injection then
container creates bean class object using 0-Param constructor
|
5. If all
properties are configured for constructor
injection then container creates bean class object using
Parameterized constructor
|
No comments:
Post a Comment