spring实战四之Bean的自动装配(注解方式)

时间:2022-03-30 20:02:39

使用注解装配:

从spring2.5开始,Spring启用了使用注解自动装配Bean的属性,使用注解方式自动装配与在XML中使用 autowire 属性自动装配并没有太大区别,但是使用注解方式允许更细粒度的自动装配。

Spring容器默认禁用注解装配。所以,在使用基于注解的自动装配前,需要在Spring配置中启用它,最简单的启用方式是使用spring的context命名空间配置中的 <context:annotation-config>元素,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config />
<!-- bean declarations go here -->
</beans>

<context:annotation-config>元素告诉Spring我们打算使用基于注解的自动装配,一旦配置完成,我们就可以对代码添加注解,标识Spring应该为属性,方法和构造器进行自动装配。

Spring3支持几种不同的用于自动装配的注解:

第一种:Spring自带的@Atutowired注解;

第二种:JSR-330的@Inject注解;

第三种:JSR-250的@Resource注解。

1.使用@Atutowired注解

使用@Atutowired注解标注setter方法:

@Autowired
public void setInstrument(Instrument instrument) {
this.instrument = instrument;
}

当Spring发现对setInstrument()方法使用了@Atutowired注解时,Spring会尝试对该方法进行byType自动装配。

@Atutowired注解有趣的地方在于,不仅可以使用它标注setter方法,还可以标注需要自动装配的Bean引用的任意方法:

@Atutowired注解标注普通方法:

@Autowired
public void heresYourInstrument(Instrument instrument) {
this.instrument = instrument;
}

@Atutowired注解标注构造器:

@Autowired
public Instrumentalist(Instrument instrument) {
this.instrument = instrument;
}

当对构造器进行标注时,@Atutowired注解当创建Bean时,即使在Spring XML中没有使用 <constructor-arg>元素配置bean,该构造器也需要进行自动装配。

@Atutowired注解标直接标注属性,并删除setter方法:

@Autowired
private Instrument instrument;

@Atutowired不会受限于private关键字。即使instrument属性是私有的实例变量。

@Atutowired的受限在于如果没有找到匹配的bean,或者存在多个匹配的Bean,庆幸的是这两种场景都有相应的解决办法。

解决没有找到匹配的bean的方式:可选的自动装配

通过设置@Atutowired的required属性为false来配置自动装配是可选的。如下:

@Autowired(required=false)
private Instrument instrument;

在上面代码中,Spring尝试装配instrument属性,但是如果没有找到与之匹配的类型为Instrument的Bean,应用就不会发生任何问题,而instrument属性的值会设置为null。

需要注意的是required虽然可以用于@Atutowired注解所使用的任意的地方,但是当使用构造器装配时,只有一个构造器可以将@Atutowired的required属性设置为true。其他使用@Atutowired注解所标注的构造器只能将required属性设置为false,此外,当使用@Atutowired标注多个构造器时,Spring就会从所有满足装配条件的构造器中选择入参最多的那个构造器。

解决在多个匹配的Bean的方式:限定歧义性的依赖

为了帮助@Atutowired鉴别出哪一个bean才是我们所需要的,可以配合使用Spring的 @Qualifier注解,如下:

@Autowired
@Qualifier("guitar")
private Instrument instrument;

在上面的代码中,即使有其他的bean也可以装配到instrument属性中,但我们可以使用@Qualifier来明确指定名为guitar的Bean。

@Qualifier注解真正的缩小了自动装配挑选候选Bean的范围。还可以通过在Bean上直接使用qualifier来缩小范围,如下所示:

<bean class="com.springinaction.springidol.Guitar">
<qualifier value="stringed" />
</bean>

除了可以在XML中指定qualifier,还可以使用@Qualifier注解来标注Guitar吉他类,如下所示:

@Qualifier("stringed")
public class Guitar implements Instrument {
...
}

创建自定义限定器注解:

为了创建一个自定义的限定器注解器,所需要做的仅仅是定义一个注解,并使用@Qualifier注解作为它的元注解。例如,当我们创建自己的@StringedInstrument注解来充当限定器,如下所示自定义的限定器注解:

package com.springinaction.springidol.qualifiers;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.beans.factory.annotation.Qualifier;
@Target({ElementType.FIELD, ElementType.PARAMETER, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Qualifier
public @interface StringedInstrument {
}

通过自定义的@StringedInstrument注解,我们现在可以用它来代替@Qualifier来标注 Guitar :

@StringedInstrument
public class Guitar implements Instrument {
...
}

现在,我们也可以使用@StringedInstrument对自动装配的instrument属性进行限定:

@Autowired
@StringedInstrument
private Instrument instrument;

当Spring尝试装配instrument属性时,Spring会把所有可选择的乐器Bean缩小到只有被@StringedInstrument注解所标注的bean。如果只有一个乐器Bean使用@StringedInstrument注解,那该Bean将会被装配到instrument属性中。

如果使用@StringedInstrument注解的乐器Bean有多个,我们还需要进一步限定来缩小范围。例如,假设除了Guitar Bean,我们还有一个  HammeredDulcimer bean 也需要@StringedInstrument注解,所以为了进一步限定为Guitar Bean,我们可以定义另一种限定器注解@Strummed:

@Target({ElementType.FIELD, ElementType.PARAMETER, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Qualifier
public @interface Strummed {
}

给Guitar增加标注:

@StringedInstrument
@Strummed
public class Guitar implements Instrument {
...
}

那么现在可以使用@Strummed注解标注instrument属性把选择范围缩小,如下所示:

@Autowired
@StringedInstrument
@Strummed
private Instrument instrument;

现在Guitar类是唯一使用@Strummed and @StringedInstru-ment的类,它将是唯一可以注入到instrument属性的bean。如果还需要细分的话,那么需要做进一步的限定。

使用Spring的@Atutowired可以减少Spring XML的配置,但是使用它的类会引入对Spring的特定依赖,即使这种依赖仅仅只是一个注解。幸运的是,Spring还提供了标准的java注解来替代@Atutowired,那就是@Inject注解。

2. JSR-330的@Inject注解

3. JSR-250的@Resource注解