使用注解装配:

从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注解

spring实战四之Bean的自动装配(注解方式)的更多相关文章

  1. spring实战二之Bean的自动装配(非注解方式)

    Bean的自动装配 自动装配(autowiring)有助于减少甚至消除配置<property>元素和<constructor-arg>元素,让Spring自动识别如何装配Bea ...

  2. Spring学习03(Bean的自动装配)

    6.Bean的自动装配 6.1 自动装配说明 自动装配是使用spring满足bean依赖的一种方法 spring会在应用上下文中为某个bean寻找其依赖的bean. Spring中bean的三种装配机 ...

  3. spring实战五之Bean的自动检测

    在spring实战四中,使用在Spring中增加<context:annotation-config>的方式告诉Spring,我们打算使用基于注解的自动装配,希望Spring特殊对待我们所 ...

  4. Spring学习--xml 中 Bean 的自动装配

    Spring IOC 容器可以自动装配 Bean. 只要在 <bean> 的 autowire 属性里指定自动装配的模式. byName(根据名称自动装配):必须将目标 Bean 的名称和 ...

  5. Spring《四-一》解决自动装配的问题

    自动化装配使得研发减少了响应的指配工作,但是使得响应的检查难以完成. 解决方法: simple模式: <bean autowire="autodetect" dependen ...

  6. Spring基础——在 Spring Config 文件中基于 XML 的 Bean 的自动装配

    一.Spring IOC 容器支持自动装配 Bean,所谓自动装配是指,不需要通过 <property> 或 <constructor-arg> 为 Bean 的属性注入值的过 ...

  7. bean的自动装配,使用注解开发,使用java的方式配置Spring

    bean的自动装配 自动装配是Spring满足bean依赖一种方式! Spring会在上下文中自动寻找,并自动给bean装配属性! 在Spring中有三种装配的方式 在xml中显示的配置 在java中 ...

  8. Spring bean的自动装配属性

    bean的自动装配属性能简化xml文件配置. bean 的自动装配属性分为四种: 1.byName 2.byTyoe 3.constructor 4. autodetect byName: 它查找配置 ...

  9. Spring(三):bean的自动装配

    Bean的自动装配 自动装配是Spring满足bean依赖的一种方式. Spring会在上下文中自动寻找,并自动给bean装配属性 Spring中三种装配方式 在xml中显式的配置. 在java中显式 ...

随机推荐

  1. 遇到个小问题,Java泛型真的是鸡肋吗?

    今天遇到一个小问题,让我感觉Java的泛型(因为背负了历史的包袱导致的)有点鸡肋啊. 我们经常会遇到要一些自定义的key-value字符串,比如: "key1:1k;key2:2;key3: ...

  2. Kik CEO Ted Livingston发博称要成为西方的微信?

    加拿大手机聊天应用Kik是一款手机通信录的社交软件,和Snapchat.微信相似,上个月刚拿到3830万美元融资.近日,Kik CEO Ted Livingston在medium博客上发表了the r ...

  3. 那些不是秘密的微信earning方法

    微信这个新兴的移动平台着实培养了一些自媒体大号,让个人也能成为媒体中心,当然微信也成为了集富利器.他们是怎么做到的呢?让我们探究一下微信earn的方法吧,一起发散思维. ★微信公众平台推广功能公测,大 ...

  4. 测试Swift语言代码高亮-使用highlight.js

    func &( left:OCBool, right: OCBool)->OCBool{ if left{ return right } else{ return false } }

  5. 使用python一步一步搭建微信公众平台(一)

    使用的工具,python 新浪SAE平台,微信的公众平台 你需要先在微信的公众平台与新浪SAE平台上各种注册,微信平台注册的时候需要你拍张手持身份证的照片,还有几天的审核期 微信公众平台:http:/ ...

  6. ruby on rails揭开route路由的真面目

    文章是从我的个人主页上粘贴过来的, 大家也可以访问我的主页 www.iwangzheng.com 最近的项目里发现一个问题,大师和pp写的index页面就好使,我写index页面就不往index页面跳 ...

  7. Centos 7 安装LAMP环境

    一.安装Centos 官网下载Centos 7刻录成光盘后安装 二.安装apache yum install httpd #根据提示,输入Y安装即可成功安装 systemctl start httpd ...

  8. poj2485 Highways

    Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public h ...

  9. iPhone取消软件更新上边的1

    去除设置的更新+1小红点提示主要分为越狱和非越狱设备两种方法. 越狱状态下方法: 首先将你的设备进行越狱: 越狱后安装ifile(这个自行搜索安装): 用ifile打开/System/Library/ ...

  10. doTjs源码研究笔记

    首先是入口方法 /*tmpl:模板文本 c:用户自定义配置 def:定义编译时执行的数据*/doT.template = function(tmpl, c, def) { } 然后进入第一句代码 c ...