Spring 注解详细介绍(三)

13 篇文章 0 订阅

使用 @Autowired 注释  

 

Spring 2.5 引入了 @Autowired 注释,它可以对类成员变量、方法及构造函数进行标注,完成自动装配的工作。来看一下使用 @Autowired 进行成员变量自动注入的代码:  

清单 6. 使用 @Autowired 注释的 Boss.java  

                  

package com.baobaotao;  

import org.springframework.beans.factory.annotation.Autowired;  

 

public class Boss {  

 

   @Autowired 

   private Car car;  

 

   @Autowired 

   private Office office;  

 

   …  

}  

Spring 通过一个 BeanPostProcessor 对 @Autowired 进行解析,所以要让 @Autowired 起作用必须事先在 Spring 容器中声明 AutowiredAnnotationBeanPostProcessor Bean。  

清单 7. 让 @Autowired 注释工作起来  

                  

<?xmlversion="1.0" encoding="UTF-8" ?>  

<beansxmlns="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.5.xsd">  

      <!-- 该BeanPostProcessor 将自动起作用,对标注 @Autowired 的 Bean 进行自动注入 -->  

    <bean class="org.springframework.beans.factory.annotation.  

       AutowiredAnnotationBeanPostProcessor"/>  

    <!-- 移除 boss Bean 的属性注入配置的信息 -->  

    <bean id="boss"class="com.baobaotao.Boss"/>  

       <bean id="office"class="com.baobaotao.Office">  

        <property name="officeNo"value="001"/>  

    </bean>  

    <bean id="car"class="com.baobaotao.Car" scope="singleton">  

        <property name="brand"value=" 红旗 CA72"/>  

        <property name="price"value="2000"/>  

    </bean>  

</beans>  

这样,当 Spring 容器启动时,AutowiredAnnotationBeanPostProcessor 将扫描 Spring 容器中所有 Bean,当发现 Bean 中拥有 @Autowired 注释时就找到和其匹配(默认按类型匹配)的 Bean,并注入到对应的地方中去。  

按照上面的配置,Spring将直接采用 Java 反射机制对 Boss 中的 car 和 office 这两个私有成员变量进行自动注入。所以对成员变量使用 @Autowired后,您大可将它们的setter 方法(setCar() 和 setOffice())从 Boss 中删除。  

 当然,您也可以通过 @Autowired 对方法或构造函数进行标注,来看下面的代码:  

清单 8. 将 @Autowired 注释标注在 Setter 方法上  

                  

package com.baobaotao;  

 

public class Boss {  

   private Car car;  

   private Office office;  

 

     @Autowired 

   public void setCar(Car car) {  

       this.car = car;  

   }  

   

   @Autowired 

   public void setOffice(Office office) {  

       this.office = office;  

   }  

   …  

}  

 

这时,@Autowired 将查找被标注的方法的入参类型的 Bean,并调用方法自动注入这些 Bean。而下面的使用方法则对构造函数进行标注:  

 

清单 9. 将 @Autowired 注释标注在构造函数上  

                  

package com.baobaotao;  

 

public class Boss {  

   private Car car;  

   private Office office;  

   

   @Autowired 

   public Boss(Car car ,Office office){  

       this.car = car;  

       this.office = office ;  

   }  

   

   …  

}  

 

由于 Boss() 构造函数有两个入参,分别是 car 和 office,@Autowired 将分别寻找和它们类型匹配的 Bean,将它们作为 Boss(Car car ,Office office) 的入参来创建 Boss Bean。  

 

 

当候选 Bean 数目不为 1 时的应对方法  

 

在默认情况下使用@Autowired 注释进行自动注入时,Spring容器中匹配的候选 Bean 数目必须有且仅有一个。当找不到一个匹配的 Bean 时,Spring 容器将抛出 BeanCreationException 异常,并指出必须至少拥有一个匹配的 Bean。我们可以来做一个实验:  

 

清单 10. 候选 Bean 数目为 0 时  

                  

<?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.5.xsd">  

   

    <beanclass="org.springframework.beans.factory.annotation.  

       AutowiredAnnotationBeanPostProcessor"/>   

 

   <bean id="boss"class="com.baobaotao.Boss"/>  

 

   <!-- 将 office Bean 注释掉 -->  

   <!-- <bean id="office"class="com.baobaotao.Office">  

   <property name="officeNo" value="001"/>  

   </bean>-->  

 

   <bean id="car" class="com.baobaotao.Car"scope="singleton">  

       <property name="brand" value=" 红旗 CA72"/>  

       <property name="price" value="2000"/>  

   </bean>  

</beans>  

 

由于 office Bean被注释掉了,所以Spring 容器中将没有类型为Office 的 Bean 了,而 Boss 的 office 属性标注了 @Autowired,当启动 Spring 容器时,异常就产生了。  

 

当不能确定 Spring 容器中一定拥有某个类的 Bean 时,可以在需要自动注入该类 Bean 的地方可以使用 @Autowired(required = false),这等于告诉 Spring:在找不到匹配 Bean 时也不报错。来看一下具体的例子:  

 

清单 11. 使用 @Autowired(required = false)  

                  

package com.baobaotao;  

 

importorg.springframework.beans.factory.annotation.Autowired;  

import org.springframework.beans.factory.annotation.Required;  

 

public class Boss {  

 

   private Car car;  

   private Office office;  

 

   @Autowired 

   public void setCar(Car car) {  

       this.car = car;  

   }  

   @Autowired(required = false)  

   public void setOffice(Office office) {  

       this.office = office;  

   }  

   …  

}  

   

 

 

当然,一般情况下,使用@Autowired 的地方都是需要注入 Bean的,使用了自动注入而又允许不注入的情况一般仅会在开发期或测试期碰到(如为了快速启动 Spring 容器,仅引入一些模块的 Spring 配置文件),所以 @Autowired(required = false) 会很少用到。  

 

和找不到一个类型匹配Bean 相反的一个错误是:如果Spring 容器中拥有多个候选 Bean,Spring 容器在启动时也会抛出 BeanCreationException 异常。来看下面的例子:  

 

清单 12. 在 beans.xml 中配置两个 Office 类型的 Bean  

                  

…   

<bean id="office"class="com.baobaotao.Office">  

   <property name="officeNo" value="001"/>  

</bean>  

<bean id="office2"class="com.baobaotao.Office">  

   <property name="officeNo" value="001"/>  

</bean>  

…  

 

我们在 Spring 容器中配置了两个类型为 Office 类型的 Bean,当对 Boss 的 office 成员变量进行自动注入时,Spring 容器将无法确定到底要用哪一个 Bean,因此异常发生了。  

 

Spring 允许我们通过 @Qualifier 注释指定注入 Bean 的名称,这样歧义就消除了,可以通过下面的方法解决异常:  

 

 

清单 13. 使用 @Qualifier 注释指定注入 Bean 的名称  

                  

@Autowired 

public voidsetOffice(@Qualifier("office")Office office) {  

   this.office = office;  

}  

   

 

 

@Qualifier("office") 中的 office 是 Bean 的名称,所以 @Autowired 和 @Qualifier 结合使用时,自动注入的策略就从 byType 转变成 byName 了。@Autowired 可以对成员变量、方法以及构造函数进行注释,而 @Qualifier 的标注对象是成员变量、方法入参、构造函数入参。正是由于注释对象的不同,所以 Spring 不将 @Autowired 和 @Qualifier 统一成一个注释类。下面是对成员变量和构造函数入参进行注释的代码:   

 

对成员变量进行注释:  

 

 

清单 14. 对成员变量使用 @Qualifier 注释  

                  

public class Boss {  

   @Autowired 

   private Car car;  

   

   @Autowired 

   @Qualifier("office")  

   private Office office;  

   …  

}  

对构造函数入参进行注释:  

清单 15. 对构造函数变量使用 @Qualifier 注释  

                  

public class Boss {  

   private Car car;  

    privateOffice office;  

 

   @Autowired 

   public Boss(Car car , @Qualifier("office")Office office){  

       this.car = car;  

       this.office = office ;  

   }  

}  

   

 

@Qualifier 只能和 @Autowired 结合使用,是对 @Autowired 有益的补充。一般来讲,@Qualifier 对方法签名中入参进行注释会降低代码的可读性,而对成员变量注释则相对好一些。  


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

趙大叔

你的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值