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

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

自己简述:byName,就是要设置自动装载为byName的Bean,如果该BEAN中有某一个属性,那么,它就会在该配置文件XML中,找到与之相同ID或者NAME的BEAN,让这个BEAN装载原BEAN。

根据属性名自动装配。此选项将检查容器并根据名字查找与属性完全一致的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)