Spring自动装配模式二:byName的解析

Spring自动装配模式二:byName的解析
根据属性名自动装配。此选项将检查容器并根据名字查找与属性完全一致的bean,并将其与属性自动装配。例如,在bean定义中将autowire设置为byName,而该bean包含homeAddressServiceImpl属性(同时提供setHomeAddressServiceImpl(..)方法),Spring就会查找名为homeAddressServiceImpl的bean定义,并用它来装配给homeAddressServiceImpl属性。
配置文件如下:
<bean id="homeAddressServiceImpl" class="cn.csdn.service.HomeAddressServiceImpl"
scope="singleton">
<property name="address">
<value>北京海淀上地软件园</value>
</property>
</bean>
<bean id="empServiceImpl" class="cn.csdn.service.EmpServiceImpl"
scope="singleton" autowire="byName" />

cn.csdn.service.HomeAddressServiceImpl代码
public class HomeAddressServiceImpl extends AddressServiceImpl {

private String address;

public void setAddress(String address){
this.address=address;
}



public HomeAddressServiceImpl() {
super();
}



public HomeAddressServiceImpl(String address){
this.address=address;
}

}

cn.csdn.service.EmpServiceImpl 代码
package cn.csdn.service;
public class EmpServiceImpl {

private HomeAddressServiceImpl homeAddressServiceImpl;

public void setHomeAddressServiceImpl(
HomeAddressServiceImpl homeAddressServiceImpl) {
this.homeAddressServiceImpl = homeAddressServiceImpl;
}

}

你可能感兴趣的:(spring,bean)