Auto wiring introduction with xml and annotation
-> it is used to inject dependencies automatically
-> it can't be used to inject primitive and string values, it works with reference only.
Object can be decleared in two ways:
"When using byType"
-> it can't be used to inject primitive and string values, it works with reference only.
It can be done by two ways:
1)XML
2)ANNOTATION
With XML:
Autowiring can be done in
a)byName
b)byType
c)constructor
d)autodetect--It is deprecated since spring 3
With Annotations:
a)Autowired
b)Qualifier
Autowiring of Bean in xml
Object can be decleared in two ways:
This two are same: Either cast the bean or give the bean class:Emp emp = context.getBean("emp", Emp.class);
Emp emp = (Emp)context.getBean("emp);
"When using byName"
When giving the Autowired property to reference variable class.
The bean name and the object name should be similar
e.g
private Address address;
<bean class="some class" name="address"/>
This both address should be same. it will not give error, but gives a null.
"When using byType"
There will be ambiguity problem. So better to use byName
When using byName and byType, setter injection is used where as when using constructor , constructor injection is used.
Auto Wiring with Annotation
->@Autowired can be used above property, setter and constructor. 3 ways of autowiring
->@Qualifier("address") is used to specify the name of the bean
@Autowired
@Qualifier("address")
Comments
Post a Comment