职业IT人-IT人生活圈

 找回密码
 成为会员
搜索
查看: 999|回复: 2

Spring 中Bean的自动装配六种模式其四

[复制链接]
木已 发表于 2011-9-3 10:11 | 显示全部楼层 |阅读模式
   Spring 中Bean的自动装配六种模式其四
Spring IoC容器可以自动装配(autowire)相互协作bean之间的关联关系。因此,如果可能的话,可以自动让Spring通过检查BeanFactory中的内容,来替我们指定bean的协作者(其他被依赖的bean)。autowire一共有六种类型。由于autowire可以针对单个bean进行设置,因此可以让有些bean使用autowire,有些bean不采用。autowire的方便之处在减少或者消除属性或构造器参数的设置,这样可以给我们的配置文件减减肥![2] 在xml配置文件中,可以在<bean/>元素中使用autowire属性指定:
模式说明
Constructor与byType的方式类似,不同之处在于它应用于构造器参数。如果在容器中没有找到与构造器参数类型一致的bean,那么将会抛出异常。

下来我们就用案例来证明一下:准备3个类:


  
public class AddressServiceImpl {   
  
/**住址*/  
  
private String address;   
  
public void setAddress(String address){   
  
this.address=address;   
  
}   
  
}   
  
  
  
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;   
  
}   
  
  
}   
  
  
  
public class EmpServiceImpl {   
  
  
/**公司地址*/  
  
private AddressServiceImpl companyAddress;   
  
  
  
public void setCompanyAddress(AddressServiceImpl companyAddress){   
  
this.companyAddress=companyAddress;   
  
}   
  
}  

public class AddressServiceImpl {

/**住址*/

private String address;

public void setAddress(String address){

this.address=address;

}

}



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;

}


}



public class EmpServiceImpl {


/**公司地址*/

private AddressServiceImpl companyAddress;



public void setCompanyAddress(AddressServiceImpl companyAddress){

this.companyAddress=companyAddress;

}

}


Constructor值的constructor.xml配置文件
  Constructor:与byType的方式类似,不同之处在于它应用于构造器参数。如果在容器中没有找到与构造器参数类型一致的bean,那么将会抛出异常。

  
Xml代码  
   
  
<?xml version="1.0" encoding="UTF-8"?>  
  
<beans xmlns="http://www.springframework.org/schema/beans"  
  
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">   
  
<!-- 配置bean  相同类型只能在 配置文件中出现一次   
  
<bean id="homeAddressServiceImpl" class="cn.csdn.service.HomeAddressServiceImpl" scope="prototype">  
  
  <property name="address">  
  
    <value>北京</value>  
  
  </property>  
  
</bean>  
  
<!-- 自动装配 采用constructor 构造器中的参数是按照byType进行装配的   
  
<bean id="empServiceImpl" class="cn.csdn.service.EmpServiceImpl" scope="singleton" autowire="constructor"/>  
  
</beans>  



<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

<!-- 配置bean  相同类型只能在 配置文件中出现一次

<bean id="homeAddressServiceImpl" class="cn.csdn.service.HomeAddressServiceImpl" scope="prototype">

  <property name="address">

    <value>北京</value>

  </property>

</bean>

<!-- 自动装配 采用constructor 构造器中的参数是按照byType进行装配的

<bean id="empServiceImpl" class="cn.csdn.service.EmpServiceImpl" scope="singleton" autowire="constructor"/>

</beans>


测试类:(junit测试)  
  
  public class App {   
  
@Test  
  
public void test(){   
  
ApplicationContext ac=new ClassPathXmlApplicationContext("classpath:byName.xml"); EmpServiceImpl emp = (EmpServiceImpl) ac.getBean("empServiceImpl");   
  
}   
  
}  

  public class App {

@Test

public void test(){

ApplicationContext ac=new ClassPathXmlApplicationContext("classpath:byName.xml"); EmpServiceImpl emp = (EmpServiceImpl) ac.getBean("empServiceImpl");

}

}

<!--EndFragment-->

话说我当年 发表于 2011-9-3 10:11 | 显示全部楼层
这就是构造器类 的,自动装载

只学java 发表于 2011-9-3 10:11 | 显示全部楼层
总共是6种模式 你可以参考http://javawangli.iteye.com/admin/blogs/1010764
您需要登录后才可以回帖 登录 | 成为会员

本版积分规则

QQ|手机版|小黑屋|网站帮助|职业IT人-IT人生活圈 ( 粤ICP备12053935号-1 )|网站地图
本站文章版权归原发布者及原出处所有。内容为作者个人观点,并不代表本站赞同其观点和对其真实性负责,本站只提供参考并不构成任何投资及应用建议。本站是信息平台,网站上部分文章为转载,并不用于任何商业目的,我们已经尽可能的对作者和来源进行了通告,但是能力有限或疏忽造成漏登,请及时联系我们,我们将根据著作权人的要求立即更正或者删除有关内容。

GMT+8, 2024-5-3 15:29 , Processed in 0.102285 second(s), 20 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表