Spring_ Spring_教程9_Spring中Bean的自动装配六种模式


  

       Spring IoC容器可以自动装配(autowire)相互协作bean之间的关联关系。因此,如果可能的话,可以自动让Spring通过检查BeanFactory中的内容,来替我们指定bean的协作者(其他被依赖的bean)。autowire一共有种类型。由于autowire可以针对单个bean进行设置,因此可以让有些bean使用autowire,有些bean不采用。autowire的方便之处在减少或者消除属性或构造器参数的设置,这样可以给我们的配置文件减减肥![2] 在xml配置文件中,可以在<bean/>元素中使用autowire属性指定:

 

 

 

模式

说明

  Default

在每个bean中都一个autowire=default的默认配置它的含义是:

采用beans和跟标签中的default-autowire="属性值"一样的设置。

 

  No

不使用自动装配,必须通过ref元素指定依赖,默认设置。

 

  ByNname

根据属性名自动装配。此选项将检查容器并根据名字查找与属性完全一致的bean,并将其与属性自动装配。例如,在bean定义中将autowire设置为by name,而该bean包含master属性(同时提供setMaster(..)方法),Spring就会查找名为master的bean定义,并用它来装配给master属性。

  ByType

如果容器中存在一个与指定属性类型相同的bean,那么将与该属性自动装配。如果存在多个该类型的bean,那么将会抛出异常,并指出不能使用byType方式进行自动装配。若没有找到相匹配的bean,则什么事都不发生,属性也不会被设置。如果你不希望这样,那么可以通过设置dependency-check="objects"让Spring抛出异常。

 

 Constructor

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

Antodetect

通过bean类的自省机制(introspection)来决定是使用constructor还是byType方式进行自动装配。如果发现默认的构造器,那么将使用byType方式

 

 

 

 

 

 

 

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

 

 

Java代码   收藏代码
  1. public class AddressServiceImpl {   
  2.   
  3. /**住址*/  
  4.   
  5. private String address;   
  6.   
  7. public void setAddress(String address){  
  8.   
  9. this.address=address;  
  10.   
  11. }  
  12.   
  13. }  
  14.   
  15.   
  16.   
  17. public class HomeAddressServiceImpl extends AddressServiceImpl {  
  18.   
  19.   
  20. private String address;  
  21.   
  22.   
  23. public void setAddress(String address){  
  24.   
  25. this.address=address;  
  26.   
  27. }  
  28.   
  29.   
  30. public HomeAddressServiceImpl() {  
  31.   
  32. super();  
  33.   
  34. }  
  35.   
  36. public HomeAddressServiceImpl(String address){  
  37.   
  38. this.address=address;  
  39.   
  40. }  
  41.   
  42.   
  43. }  
  44.   
  45.   
  46.   
  47. public class EmpServiceImpl {  
  48.   
  49.   
  50. /**公司地址*/  
  51.   
  52. private AddressServiceImpl companyAddress;  
  53.   
  54.   
  55.   
  56. public void setCompanyAddress(AddressServiceImpl companyAddress){  
  57.   
  58. this.companyAddress=companyAddress;  
  59.   
  60. }  
  61.   
  62. }  

 

1,default和nodefault.xml配置文件

 

 

Xml代码   收藏代码
  1.  <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <beans xmlns="http://www.springframework.org/schema/beans"  
  4.   
  5. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  6.   
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"  
  8.   
  9. default-autowire="no">  
  10.   
  11. <bean id="homeAddressServiceImpl" class="cn.csdn.service.HomeAddressServiceImpl"  
  12.   
  13. scope="singleton">  
  14.   
  15. <property name="address">  
  16.   
  17. <value>北京海淀上地软件园</value>  
  18.   
  19. </property>   
  20.   
  21. </bean>  
  22.   
  23. <bean id="empServiceImpl" class="cn.csdn.service.EmpServiceImpl"  
  24.   
  25. scope="singleton" autowire="default" />  
  26.   
  27. </beans>  

 

 

 

 

 

 测试类:(junit测试)  

 

 

Java代码   收藏代码
  1.  public class App {   
  2.   
  3. @Test  
  4.   
  5. public void test(){  
  6.   
  7. ApplicationContext ac= new ClassPathXmlApplicationContext("classpath:default.xml"); EmpServiceImpl emp = (EmpServiceImpl) ac.getBean("empServiceImpl");  
  8.   
  9. }  
  10.   
  11. }  

 

 声明:

<bean id="empServiceImpl" class="cn.csdn.service.EmpServiceImpl"

scope="singleton" autowire="no" />

不使用自动装配,必须通过ref元素指定依赖,默认设置。

 

  

 

 

 

2,byName值的byname.xml配置文件

 

 

Xml代码   收藏代码
  1.  <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <beans xmlns="http://www.springframework.org/schema/beans"  
  4.   
  5. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  6.   
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">  
  8.   
  9. <bean id="homeAddressServiceImpl" class="cn.csdn.service.HomeAddressServiceImpl"  
  10.   
  11. scope="singleton">  
  12.   
  13. <property name="address">  
  14.   
  15. <value>北京海淀上地软件园</value>  
  16.   
  17. </property>   
  18.   
  19. </bean>  
  20.   
  21. <bean id="empServiceImpl" class="cn.csdn.service.EmpServiceImpl"  
  22.   
  23. scope="singleton" autowire="byName" />  
  24.   
  25. </beans>  

 

 

 

 测试类:(junit测试)  

 

Java代码   收藏代码
  1.  public class App {   
  2.   
  3. @Test  
  4.   
  5. public void test(){  
  6.   
  7. ApplicationContext ac=new ClassPathXmlApplicationContext("classpath:byName.xml"); EmpServiceImpl emp = (EmpServiceImpl) ac.getBean("empServiceImpl");  
  8.   
  9. }  
  10.   
  11. }  

 

 

3,byType值bytype.xml配置文件

 

 

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <beans xmlns="http://www.springframework.org/schema/beans"  
  4.   
  5. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  6.   
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">  
  8.   
  9. <bean id="homeAddressServiceImpl" class="cn.csdn.service.HomeAddressServiceImpl"  
  10.   
  11. scope="singleton">  
  12.   
  13. <property name="address">  
  14.   
  15. <value>北京海淀上地软件园</value>  
  16.   
  17. </property>   
  18.   
  19. </bean>  
  20.   
  21.   
  22. <bean id="empServiceImpl" class="cn.csdn.service.EmpServiceImpl"  
  23.   
  24. scope="singleton" autowire="byType" />  
  25.   
  26.   
  27.   
  28. </beans>  

 

 

 

 

注意异常:

 

 

Xml代码   收藏代码
  1. <beanidbeanid="addressServiceImpl"class="cn.csdn.service.AddressServiceImpl" scope="singleton"/>  
  2.   
  3. //homeAddressServiceImpl是继承addressServiceImpl,所以他们是同一类型!  
  4.   
  5.   
  6. <!--  当有多个相同类型的bean时,会出现bug如下:  
  7.   
  8.    org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'empServiceImpl' defined in file [D:\Workspaces\MyEclipse 8.6\20110419_01\bin\applicationContext.xml]: Unsatisfied dependency expressed through bean property 'companyAddress': : No unique bean of type [cn.csdn.service.AddressServiceImpl] is defined: expected single matching bean but found 2: [homeAddressServiceImpl, addressServiceImpl]; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [cn.csdn.service.AddressServiceImpl] is defined: expected single matching bean but found 2: [homeAddressServiceImpl, addressServiceImpl]  
  9.   
  10. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireByType(AbstractAutowireCapableBeanFactory.java:1091)  
  11.   
  12. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:982)  
  13.   
  14. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:472)  
  15.   
  16. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)  
  17.   
  18. at java.security.AccessController.doPrivileged(Native Method)..........................  
  19.   
  20. -->  

 

 

 测试类:(junit测试)  

     

Java代码   收藏代码
  1.  public class App {   
  2.   
  3. @Test  
  4.   
  5. public void test(){  
  6.   
  7. ApplicationContext ac=new ClassPathXmlApplicationContext("classpath:byName.xml"); EmpServiceImpl emp = (EmpServiceImpl) ac.getBean("empServiceImpl");  
  8.   
  9. }  
  10.   
  11. }  

 

注意:

   byName 和byType

   在使用的过程中必须保证bean能够初始化,否则的话会出现bug

   如果有默认的无参数的构造器就不需要多余的配置

   如果有带有参数的构造器,那在bean的配置中必须配置器初始化的参数 或者在bean中添加无参数的构造器

 

 

4, Constructor值的constructor.xml配置文件

 

 

 

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <beans xmlns="http://www.springframework.org/schema/beans"  
  4.   
  5. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  6.   
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">   
  8.   
  9. <!-- 配置bean  相同类型只能在 配置文件中出现一次  
  10.   
  11. <bean id="homeAddressServiceImpl" class="cn.csdn.service.HomeAddressServiceImpl" scope="prototype">  
  12.   
  13.   <property name="address">  
  14.   
  15.     <value>北京</value>  
  16.   
  17.   </property>  
  18.   
  19. </bean>  
  20.   
  21. <!-- 自动装配 采用constructor 构造器中的参数是按照byType进行装配的  
  22.   
  23. <bean id="empServiceImpl" class="cn.csdn.service.EmpServiceImpl" scope="singleton" autowire="constructor"/>  
  24.   
  25. </beans>  

 

 

 测试类:(junit测试)  

     

Java代码   收藏代码
  1.  public class App {   
  2.   
  3. @Test  
  4.   
  5. public void test(){  
  6.   
  7. ApplicationContext ac=new ClassPathXmlApplicationContext("classpath:byName.xml"); EmpServiceImpl emp = (EmpServiceImpl) ac.getBean("empServiceImpl");  
  8.   
  9. }  
  10.   
  11. }  

 

5,Antodetect值的antodetect.xml配置文件

 

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <beans xmlns="http://www.springframework.org/schema/beans"  
  4.   
  5. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  6.   
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans    http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">   
  8.   
  9.   
  10. <!-- 配置bean -->  
  11.   
  12. <bean id="addressServiceImpl" class="cn.csdn.service.AddressServiceImpl" scope="singleton">  
  13.   
  14.   <property name="address">  
  15.   
  16.     <value>北京</value>  
  17.   
  18.   </property>  
  19.   
  20. </bean>  
  21.   
  22. <bean id="empServiceImpl" class="cn.csdn.service.EmpServiceImpl" scope="singleton" autowire="autodetect"/>  
  23.   
  24. </beans>  

 

测试类:(junit测试)  

 

Java代码   收藏代码
  1.  public class App {   
  2.   
  3. @Test  
  4.   
  5. public void test(){  
  6.   
  7. ApplicationContext ac=new ClassPathXmlApplicationContext("classpath:byName.xml"); EmpServiceImpl emp = (EmpServiceImpl) ac.getBean("empServiceImpl");  
  8.   
  9. }  
  10.   
  11. }  

 

 

 

  

 

  结束语:  

 大部分学着都认为Bean的自动装配有5种模式,但详细的说是六种模式,他们往往忽略了default 值的应用,在每个bean中都一个autowire=default的默认配置它的含义是:采用beans和跟标签中的default-autowire="属性值"一样的设置,这个值是不能忽略的!

*可以设置bean使自动装配失效:
采用xml格式配置bean时,将<bean/>元素的autowire-candidate属性设置为false,这样容器在查找自动装配对象时,将不考虑该bean,即它不会被考虑作为其它bean自动装配的候选者,但是该bean本身还是可以使用自动装配来注入其它bean的。

 

<!--EndFragment-->

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值