Spring核心——注解自动装载

全文共 2478 个字

从配置上扩展

之前的文章介绍了Spring的IoC容器配置管理方面的详细内容,需要了解的可以从IoC容器的设计模式开始阅读。在介绍基于注解配置的配置之前我们再重复一下在之前提到的基本认识:

  1. Spring的基本工作单位是Bean,所有的高级功能都是在Bean的基础上扩展而来的。Bean可以理解成Java类的一个实例。
  2. Bean只是一个个体,Spring用一个名为IoC(Inversion of Control控制反转)的容器来管理所有的Bean。
  3. Spring的核心功能就是管理Bean与Bean之间、IoC容器与Bean之间的依赖、组合关系。这些关系通过XML配置来定义。

基于以上3点,对XML配置有清晰的理解对Spring核心框架的使用至关重要。在Spring没有注解(Annotation)之前,我们都是通过XML配置来实现Spring的功能,而在3.x版本之后,我们可以仅使用注解而无需XML即可运行Spring。注解并没有扩展Spring的核心功能,他仅仅是将原来XML上的配置迁移到Java源码中以“元数据”(bytecode metadata)的方式提供非侵入式(non-invasive)的框架服务。所以无论XML配置还是注解的方式,或者两者混合使用都可以实现Spring提供的所有功能。

在之前IOC功能扩展点一文中介绍了BeanDefinition的适配器模式。如下图,了解BeanDefinition的作用之后就能明白无论是注解还是XML配置,最后都会转化为一个BeanDefinition结构让IoC容器来执行初始化。

Spring核心——注解自动装载

Spring的注解相关的功能是在2.x版本开始出现然后到3.x才全部完善的,支持JCP制定的JSR-250和JSR-330。所以在使用注解的时候需要注意版本号。

启用Annotation配置功能

在使用注解功能之前要告诉IoC现在需要启用注解相关的功能,通过上下文级别的配置即可开启所有注解相关的功能:

<?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.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

    <context:annotation-config/>

</beans>

官配后置处理器一文已经介绍了注解的处理都是通过后置处理器实现的,所以添加了<context:annotation-config/>这个配置之后在实现层面会向IoC容器增加AutowiredAnnotationBeanPostProcessor,CommonAnnotationBeanPostProcessor,PersistenceAnnotationBeanPostProcessorRequiredAnnotationBeanPostProcessor这几个后设置处理器。每个处理器都针对某一个或者某些注解进行处理。

还有,如果在工程中混合使用注解和XML配置,如果同一个Bean同时在XML和注解都进行了配置,那么最终生效的是XML上的配置,因为Spring容器会先处理注解再处理XML配置。

下面是关于自动装载的注解介绍:

@Autowired

这个注解应该是使用spring最常用的注解,也是IoC容器反向依赖注入的极致体现。基本上容器里有什么实现我们根本不必操心,之需要声明一个接口加一个@Autowired就可以获得对应的接口功能。如果不使用参数的话@Autowired的效果就相当于BeanFactory.getBean(Class<T> )。

多种方法注入数据

@Autowired可以直接写在域(成员变量)上、可以用在一般的方法和构造方法上:

interface A {}
interface B {}
interface C {}

class MyClass {
	@Autowired //从成员变量注入
	private A a;
	private B b;
	private C c;

	@Autowired //从构造方法注入
	public MyClass(C c) {
		this.c = c;
	}
	public A getA() {
		return a;
	}
	public void setA(A a) {
		this.a = a;
	}
	public B getB() {
		return b;
	}
	@Autowired //从普通方法注入
	public void setB(B b) {
		this.b = b;
	}
	public C getC() {
		return c;
	}
	public void setC(C c) {
		this.c = c;
	}
}

获取同一个接口的多个实现

如果容器中同一个接口有相同的实现,我们可以用数据、列表或Map结构来获取多个Bean:

interface A {}
class implA1 implements A{}
class implA2 implements A{}

class MyClass {
	@Autowired
	private A[] a;
	@Autowired
	private Set<A> set;
	@Autowired
	private Map<String, A> map;
}

使用Map时,key必须声明为String,在运行时会key是注入Bean的name/id。

声明非必要数据

当我们使用@Autowired时,如果容器中没有我们所需的Bean会抛出异常。当这个Bean并不是必要数据时可以使用@Autowired的required参数:

interface A {}
class MyClass {
	@Autowired(required=false)
	private A a;
}

自动空指针处理

在Java8之后专门为空指针处理添加了Optional这个工具类。在4.x之后Spring在注入数据阶段会根据目标对象自动进行包装:

interface A {}
class impl implements A{}
class MyClass {
	@Autowired(required=false)
	private Optional<A> a;
}

这个时候在容器中存放的是接口A的实现类,但是会自定根据注入对象的声明新建一个Optional包装的Bean。

在5.x版本之后还可以使用JSR-305提出的@NullAble告诉IoC这里可以注入一个空指针数据或什么也不需要。

interface A {}
class impl implements A{}
class MyClass {
	private A a;
	public void setA(@Nullable A a) {
		this.a = a;
	}
}

获取容器中的所有资源

除了我们自己声明的接口、类,@Autowired还可以获取Spring定义的所有Bean,凡是只要在IoC容器中的Bean都可以通过它来获取:

class MyClass {
	@Autowired
	private ApplicationContext context;
}

不过需要注意的是,这些注解(Annotation)支持的功能都是由后置处理器实现的,所以无法自动注入后置处理器(BeanPostProcessor或BeanFactoryPostProcessor )。

JSR-330支持

JSR-330提出了反向依赖注入的相关内容,主要是关于@Inject、@ManagedBean、@Singleton的作用和实现方式。使用@Inject可以替换@Autowired的绝大部分功能,但是还有些许的差异。使用JSR-330要引入javax.inject包,maven的配置如下:

<dependency>
    <groupId>javax.inject</groupId>
    <artifactId>javax.inject</artifactId>
    <version>1</version>
</dependency>

然后可以用@Inject注解任何使用@Autowired的地方,唯一的区别是@Inject没有required参数,如果要实现对应的功能只能通过注入Optional<Class<T>>的方式实现。

对自动装配的控制

@Autowired虽然好用,但是也会遇到一些问题,比如当容器中有2个类实现同一个接口的时候在运行时注入就会抛出异常,针对这个问题Spring提供了一些针对自动装配更细节的操作——Primary和Qualifiers。

Primary控制自动装配

Primary字面意思就是主要的,意思是告诉容器这个Bean是“主”Bean。实现Primary有两种方式,通过在@Configuration中注解实现或在XML配置中实现。

首先是配置方式。有一个接口A、有2个A的实现类ImplFirst和ImplSecond,然后在功能类MyClass中自动装配了接口A:

interface A {}
class ImplFirst implements A{}
class ImplSecond implements A{}
class MyClass {
	@Autowired
	private A a;
}

在配置文件中我们可以使用primary属性来控制注入哪一个实现类。

<beans>
    <context:annotation-config/>
    <!-- 自动装配时会使用这个Bean -->
    <bean class="x.y.ImplFirst" primary="true"/>
    <bean class="x.y.ImplSecond"/>
    <bean class="x.y.MyClass">
</beans>

此外,我们还可是使用Java配置功能(@Configuration相关的Java配置内容后续篇幅会介绍)指明“主”Bean。将XML配置文件替换为下面的Java配置形式:

@Configuration
class MyClassConfig {
	@Bean
	@Primary
	public A firstImpl() {
		return new ImplFirst();
	}
	@Bean
	public A secondImpl() {
		return new ImplSecond();
	}
}

Qualifiers控制自动装配

Primary是类的实现者决定使用那个一个类,而Qualifiers是让类的使用者来确定使用哪一个类。先看一个最基本的用法:

interface A {}
class ImplFirst implements A {}
class ImplSecond implements A {}
class MyClass {
	@Autowired
	@Qualifier("implSecond")
	private A a;
}
<beans>
    <context:annotation-config/>
    <bean id="implFirst" class="x.y.ImplFirst"/>
    <bean id="implSecond" class="x.y.ImplSecond"/>
    <bean class="x.y.MyClass">
</beans>

这样通过@Qualifiers注解就制定加载了ImplFIrst这个类。不过Spring官方并不建议这样去使用Qualifiers。主要的原因是失去了@Autowired的使用初衷——在使用的时候还需要去了解<bean>的定义结构。官方建议通过别名的形式告知每一个类的作用,然后通过Qualifiers来使用。例如我们还是用组装电脑的例子:

interface Cpu {}
class Athlon implements Cpu {
	private String brand = "Amd";
}
class Celeron implements Cpu {
	private String brand = "Intel";
}

class MyPc {
	@Autowired
	@Qualifier("Intel")
	private Cpu a;
}

class YourPc {
	@Autowired
	@Qualifier("Amd")
	private Cpu a;
}
<beans>
    <context:annotation-config/>
    <bean id="athlon" class="x.y.Athlon">
        <qualifier value="Amd"/>
    </bean>
    <bean id="celeron" class="x.y.Celeron">
        <qualifier value="Intel"/>
    </bean>
    <bean class="x.y.MyPc"/>
    <bean class="x.y.YourPc"/>
</beans>

<qualifier value="Intel"/>可以声名当前的Bean对应的Qualifier的名称。这样完全就将Id和Qualifer定义的名称隔离开,我们可以使用规范来约定使用的功能内容。

还可以通过继承@Qualifier来实现我们更细节的控制和管理,我们将上面的代码进行如下修改:

@Target({ ElementType.FIELD, ElementType.PARAMETER })
@Retention(RetentionPolicy.RUNTIME)
@Qualifier
@interface CpuType {
	String value();
}
interface Cpu {
}
class Athlon implements Cpu {
	private String brand = "Amd";
}
class Celeron implements Cpu {
	private String brand = "Intel";
}
class MyPc {
	@Autowired
	@CpuType("Intel")
	private Cpu a;
}
class YourPc {
	@Autowired
	@CpuType("Amd")
	private Cpu a;
}
<beans>
    <context:annotation-config/>
    <bean id="athlon" class="x.y.Athlon">
        <qualifier type="CpuType" value="Amd"/>
    </bean>
    <bean id="celeron" class="x.y.Celeron">
        <qualifier type="x.y.CpuType" value="Intel"/>
    </bean>
    <bean class="x.y.MyPc"/>
    <bean class="x.y.YourPc"/>
</beans>

在<qualifier>中type指向的注解类可以是全限定名,也可以用短编码。

我们还可以在继承的注解中使用自定义参数。例如:

@Target({ ElementType.FIELD, ElementType.PARAMETER })
@Retention(RetentionPolicy.RUNTIME)
@Qualifier
@interface CpuType {
	String brand();
    String ver();
}

配置:

<beans>
    <context:annotation-config/>
    <bean id="athlon" class="x.y.Athlon">
        <qualifier type="CpuType">
            <attribute key="brand" value="Amd"/>
            <attribute key="ver" value="4321"/>
        </qualifier>
    </bean>
    <bean class="x.y.MyPc"/>
</beans>