Spring framework provides the way to inject the bean dependencies using the autowire functionality. By default autowire functionality is disabled in spring framework. A developer explicitly define the autowiring configuration in spring configuration xml file. And this point is to remember that autowiring is only supported for object dependencies.
The difference between byType and byName autowiring is as follows :
- Autowire byType will search for a
bean in configuration file, whose id
match with the property type to be
wired whereas autowire byName will
search for a bean whose id is
matching with the property name to
be wired.
As a syntax wise difference is as follows:
package com.evon;
public class Company {
private Employee emp;
public String setEmp(Employee emp) {
this .emp = emp;
}
}
in configuration file :
<bean id="beanId1" class="com.evon.Company " autowire="byType"/>
<bean id="employee" class="com.evon.Employee ">
<property name="empName" value="Rahul" />
</bean>
In the above example container will search for a employee bean in the configuration xml file which should be of type Employee.
And following is the example of autowire byName where container will search for a bean which should be register with the id name same as filed name ('emp') in configuration file. Like
<bean id="beanId2" class="com.evon.Company" autowire="byName"/>
<bean id="emp" class="com.evon.Employee ">
<property name="empName" value="Rahul" />
</bean>
3 Comment(s)