@Autowired三种实现方式

@Autowired 在Spring2.5引入,可以对

1.成员变量

2.方法和

3. 构造函数 进行标注来完成自动装配的工作。 

无需再通过传统的在bean的xml文件中进行bean的注入配置。而是使用注解,系统自动为你注入,即隐式配置。

首先要知道:@Autowired是根据类型进行标注的,如需要按照名称进行装配,则需要配合@Qualifier使用 
进行指定包扫描的component

使用示例

创建一个Spring的配置文件

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
">
    <context:component-scan base-package="AutowiredTest"/>
    <bean id="cdPlayer" class="AutowiredTest.CDPlayer"/>
</beans

写一个测试用的接口,这里命名为光盘(CompactDisc)

public interface CompactDisc {
    void play();
}

利用注解@Component声明一个组件类。 
具体实现:

@Component
public class SgtPeppers implements CompactDisc{
    @Override
    public void play() {
        System.out.println("SgtPeppers    playing....");
    }
}

或者在构造函数上

public class CDPlayer {
    CompactDisc cd;   
    //对构造函数进行标注
    @Autowired
    public CDPlayer(CompactDisc cd){
        this.cd = cd;
    }
    public void say(){
        cd.play();
    }
}

注意:如果构造函数有两个入参,分别是 bean1 和 bean2,@Autowired 将分别寻找和它们类型匹配的 Bean

编写一个测试类,作为验证是否自动为我们注入了 CompactDisc。

public class Test {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("AutowiredBean.xml");
        CDPlayer player = (CDPlayer) context.getBean("cdPlayer");
        player.say();
    }
}

结果: SgtPeppers playing….

运行,验证成功了,系统为我们自动注入了SgtPeppers这个组件类。 
由于针对接口编程,因此很有可能多个类实现了同一个接口,这时Spring如果扫描到多个匹配对象,导致Spring不知道选择哪个好,那Spring不客气的就报错了。。。为了解决此问题,继续往下看。

装配冲突问题

如果还有个OtherPeppers类也实现CompactDisc这个接口,同时也注解为组件类,那会发生什么? 
运行发现 报错了:

org.springframework.beans.factory.BeanCreationException:
 Error creating bean with name 'cdPlayer': 
 Injection of autowired dependencies failed; 
 nested exception is org.springframework.beans.factory.BeanCreationException:
 Could not autowire method: 
 public void AutowiredTest.CDPlayer.setCompactDisc(AutowiredTest.CompactDisc); 
 nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: 
 No qualifying bean of type [AutowiredTest.CompactDisc] is defined: 
 expected single matching bean but found 2: otherPeppers,sgtPeppers

上面的问题发生了歧义性问题,解决其中的办法有多种。

1、设置首选的Component,通过@Primary进行标注(如果多处都标注,依然出现歧义性问题);

2、限定自动装配的bean,在自动装配注解出添加注解@Qualifier(“name”),其中name为bean的ID(默认bean ID为类名首字母小写);如果重命名了组件类,那么自动装配将会失败。

3、通过自定义的限定符,其实就是在组件类与自动装配处,同时注解Qualifier(“name”),name为自定义且两出相同。

4、通过使用自定义的限定符注解,例如我用使用@Cold来标注组件类和自动装配两处。那么@Cold怎么自定义呢?我们需要自定义下该注解。

@Target({ElementType.CONSTRUCTOR,ElementType.FIELD,ElementType.METHOD,ElementType,TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Qualifier
public @interface Creamy{}

拓展篇

1、@Autowired与@Resource都可以用来装配bean. 都可以写在字段上,或写在setter方法上。 
2、@Autowired默认按类型装配(这个注解是属业spring的),默认情况下必须要求依赖对象必须存在,如果要允许null 值,可以设置它的required属性为false,如:@Autowired(required=false) ,如果我们想使用名称装配可以结合@Qualifier注解进行使用,如下:

@Autowired() @Qualifier("baseDao")     
private BaseDao baseDao;    

3、@Resource(这个注解属于J2EE的),默认安照名称进行装配,名称可以通过name属性进行指定, 
如果没有指定name属性,当注解写在字段上时,默认取字段名进行按照名称查找,如果注解写在setter方法上默认取属性名进行装配。 当找不到与名称匹配的bean时才按照类型进行装配。但是需要注意的是,如果name属性一旦指定,就只会按照名称进行装配,如下所示。

@Resource(name="baseDao")     
private BaseDao baseDao;    

用 @Resource注解在字段上,且这个注解是属于J2EE的,减少了与spring的耦合。

猜你喜欢

转载自sunwonder.iteye.com/blog/2404183