Autowairing:-
Auto wiring means automatically injecting the dependencies.
(Implicit Dependency Injection)
Insead of manually configuring (Explicit DIO) the injection we
can do automatically by using autowiring.
To implements autowiring we use “autowire”
attribute in<bean> tag configuration.
To perform autowiring for a particular bean we
can use any one of the following values.
Types of Autowiring:-
a)
byName
b)
byType
c)
constructor
d)
autodetect
By using autowire attribute auto wiring is
possible only on reference type bean prototypes that mean auto wiring is not
possible on simple collection framework type properties.
Auto wiring kills the readability of spring
configuration file.
byName:-
Mainly it checks for three conditions if all
these ate valid then it injects the values by setter approach
1.
dependency bean name
2.
configured bean name
3.
setter method name
è
it dependency name is abc,bean configuration
shoud be abc and the setter name method shoud i.e setAbc(Abc abc)
è
when it finds autowire=”byName” for any bean
confuguratin the it first checks for dependency bean nam in the dependent bean.
è
Then it will checks weathdr any bean is
configured in the sprng cfg file with
the same name.
è
If it finds then it will call corresponding setter method of dependent bean.
byType :-
Mainly it checks for three conditions if all
these are valid then it injects the values by setter approach
1.
Dependency bean name
2.
configured bean name
3.
setter method argument type
è
When it finds “autowire=”byType” for any bean
configuration then it first checks for dependency bean type in the dependent
bean then after it will checks weather any bean is configured in the spring cfg
file with the same type. If it find then it will call corresponding setter method.
Constructors:-
Mainly it checks for three conditions if all
these are valid then it injects the values by setter approach
1.
Dependency bean name
2.
configured bean name
3.
constructor argument type
è
When it finds “autowire=” constructor” for any
bean configuration then it first checks for dependency bean type in the
dependent bean then after it will checks weather any bean is configured in the
spring cfg file
è
Weather any bean is configured with the type.
If it finds check for constructor
è
Which will talks dependency type if it finds
it will check for constructor.
No comments:
Post a Comment