ITeye博客

Spring Autowired(required = false) 说明

darkma · 2011-07-21 15:29 · 技术 · 编程语言 · Markdown

  1. 当候选 Bean 数目不为 1 时的应对方法
  2. 在默认情况下使用 @Autowired 注释进行自动注入时,Spring 容器中匹配的候选 Bean 数目必须有且仅有一个。当找不到一个匹配的 Bean 时,Spring 容器将抛出 BeanCreationException 异常,并指出必须至少拥有一个匹配的 Bean。我们可以来做一个实验:
  3. 清单 10. 候选 Bean 数目为 0 时
  4. <?xml version="1.0" encoding="UTF-8" ?>
  5. <beans xmlns="http://www.springframework.org/schema/beans"
  6. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans
  8. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd ">
  9. <bean class="org.springframework.beans.factory.annotation.
  10. AutowiredAnnotationBeanPostProcessor"/>
  11. <bean id="boss" class="com.baobaotao.Boss"/>
  12. <!-- 将 office Bean 注释掉 -->
  13. <!-- <bean id="office" class="com.baobaotao.Office">
  14. <property name="officeNo" value="001"/>
  15. </bean>-->
  16. <bean id="car" class="com.baobaotao.Car" scope="singleton">
  17. <property name="brand" value=" 红旗 CA72"/>
  18. <property name="price" value="2000"/>
  19. </bean>
  20. </beans>
  21. 由于 office Bean 被注释掉了,所以 Spring 容器中将没有类型为 Office 的 Bean 了,而 Boss 的 office 属性标注了 @Autowired,当启动 Spring 容器时,异常就产生了。
  22. 当不能确定 Spring 容器中一定拥有某个类的 Bean 时,可以在需要自动注入该类 Bean 的地方可以使用 @Autowired(required = false),这等于告诉 Spring:在找不到匹配 Bean 时也不报错。来看一下具体的例子:
  23. 清单 11. 使用 @Autowired(required = false)
  24. package com.baobaotao;
  25. import org.springframework.beans.factory.annotation.Autowired;
  26. import org.springframework.beans.factory.annotation.Required;
  27. public class Boss {
  28. private Car car;
  29. private Office office;
  30. @Autowired
  31. public void setCar(Car car) {
  32. this.car = car;
  33. }
  34. @Autowired(required = false)
  35. public void setOffice(Office office) {
  36. this.office = office;
  37. }
  38. …
  39. }
  40. 当然,一般情况下,使用 @Autowired 的地方都是需要注入 Bean 的,使用了自动注入而又允许不注入的情况一般仅会在开发期或测试期碰到(如为了快速启动 Spring 容器,仅引入一些模块的 Spring 配置文件),所以 @Autowired(required = false) 会很少用到。
  41. 和找不到一个类型匹配 Bean 相反的一个错误是:如果 Spring 容器中拥有多个候选 Bean,Spring 容器在启动时也会抛出 BeanCreationException 异常。来看下面的例子:
  42. 清单 12. 在 beans.xml 中配置两个 Office 类型的 Bean
  43. …
  44. <bean id="office" class="com.baobaotao.Office">
  45. <property name="officeNo" value="001"/>
  46. </bean>
  47. <bean id="office2" class="com.baobaotao.Office">
  48. <property name="officeNo" value="001"/>
  49. </bean>
  50. …
  51. 我们在 Spring 容器中配置了两个类型为 Office 类型的 Bean,当对 Boss 的 office 成员变量进行自动注入时,Spring 容器将无法确定到底要用哪一个 Bean,因此异常发生了。
  52. Spring 允许我们通过 @Qualifier 注释指定注入 Bean 的名称,这样歧义就消除了,可以通过下面的方法解决异常:
  53. 清单 13. 使用 @Qualifier 注释指定注入 Bean 的名称
  54. @Autowired
  55. public void setOffice(@Qualifier("office")Office office) {
  56. this.office = office;
  57. }
  58. @Qualifier("office") 中的 office 是 Bean 的名称,所以 @Autowired 和 @Qualifier 结合使用时,自动注入的策略就从 byType 转变成 byName 了。@Autowired 可以对成员变量、方法以及构造函数进行注释,而 @Qualifier 的标注对象是成员变量、方法入参、构造函数入参。正是由于注释对象的不同,所以 Spring 不将 @Autowired 和 @Qualifier 统一成一个注释类。下面是对成员变量和构造函数入参进行注释的代码:
  59. 对成员变量进行注释:
  60. 清单 14. 对成员变量使用 @Qualifier 注释
  61. public class Boss {
  62. @Autowired
  63. private Car car;
  64. @Autowired
  65. @Qualifier("office")
  66. private Office office;
  67. …
  68. }
  69. 对构造函数入参进行注释:
  70. 清单 15. 对构造函数变量使用 @Qualifier 注释
  71. public class Boss {
  72. private Car car;
  73. private Office office;
  74. @Autowired
  75. public Boss(Car car , @Qualifier("office")Office office){
  76. this.car = car;
  77. this.office = office ;
  78. }
  79. }
  80. @Qualifier 只能和 @Autowired 结合使用,是对 @Autowired 有益的补充。一般来讲,@Qualifier 对方法签名中入参进行注释会降低代码的可读性,而对成员变量注释则相对好一些。

评论(4)

  1. #4 楼 · shitouofIT · 2018-01-26 14:44

    :idea:

  2. #3 楼 · liuchongming · 2016-04-28 14:23

    :idea:

  3. #2 楼 · 20101975liubin · 2014-10-23 10:27

    :idea:

  4. #1 楼 · dudong0726 · 2012-08-15 14:48

    :( :( :o :) :D :shock: :x :P :twisted: :evil: :!: :idea: :arrow: :idea: :roll: :oops: :cry:

评论功能维护中,当前仅展示历史评论。