A、@Autowired

org.springframework.beans.factory.annotation.Autowired

public @interface Autowired

Marks a constructor, field, setter method or config method as to be autowired by Spring's dependency injection facilities.

标注一个构造函数,字段,setter方法或者配置方法,让它通过spring的依赖注入方法自动装填。

Only one constructor (at max) of any given bean class may carry this annotation, indicating the constructor to autowire when used as a Spring bean. Such a constructor does not have to be public.

任何一个给定的bean类只能有一个构造函数可以携带这个注释,表示这个类作为一个spring的bean使用时,这个构造器将用于自动装配。这个构造函数不是必须是public。

Fields are injected right after construction of a bean, before any config methods are invoked. Such a config field does not have to be public.

在一个bean创建之后,任何方法被调用之前,这个字段被注入。这个字段不是必须是public。

Config methods may have an arbitrary name and any number of arguments; each of those arguments will be autowired with a matching bean in the Spring container. Bean property setter methods are effectively just a special case of such a general config method. Such config methods do not have to be public.

配置方法它可以是任意名称和有任意多个参数。这个方法的任何一个参数都将使用spring容器中匹配的bean来自动装填。Bean属性的setter方法也是有效的,它是普通的配置方法的一个特例。配置方法不是必须是public。

In the case of multiple argument methods, the 'required' parameter is applicable for all arguments.

当有多个参数的方法情况下,'required'将适用于所有参数。

In case of a Collection or Map dependency type, the container will autowire all beans matching the declared value type. In case of a Map, the keys must be declared as type String and will be resolved to the corresponding bean names.

Collection或Map依赖类型情况,容器将自动装配bean匹配声明的值类型,这个Map的key必须是String,Map的values必须是已知的类型。

属性

boolean required

Declares whether the annotated dependency is required.

Defaults to true.

声明这个注释依赖是否需要,默认是true。

A.1、数组、Set、Map

当我们用@Autowired来注释数组、Set、Map时,容器将自动绑定容器中所有匹配的bean。我们在上节的例子上修改来说明这个问题。

范例1

1.Foo

对数组使用自动绑定

Java代码

  1. public class Foo {   
  2. @Autowired
  3. private Bar[] bars; 

2.配置文件

配置文件中有2Bar,故这两bean(对象)将自动装配到foo中。

Xml代码

  1. <beans>
  2. <context:annotation-config/>
  3. <bean id="foo" class="x.y.Foo" />
  4. <bean id="bar1" class="x.y.Bar">
  5. <property name="a" value="bar1" />
  6. </bean>
  7. <bean id="bar2" class="x.y.Bar">
  8. <property name="a" value="bar2" />
  9. </bean>
  10. </beans>

3.测试类

测试类中将返回结果为2。

Java代码

  1. Foo foo = (Foo) ctx.getBean("foo"); 
  2. Bar[] bars=foo.getBars(); 
  3. System.out.println(bars.length); 

Set和Map实现方法类似,就是定义时有要求,必须定义为以下格式:

Set<Bar>

Map<String,Bar>

A.2、required

上例中若容器中没有Bar,将会报错,有时我们允许自动装配的属性为null,我们就需要这样来定义:

Java代码

  1. @Autowired(required=false) 
  2. private Bar bar; 

这样若容器中没有Bar,它将自动装配null。

B、@Qualifier

org.springframework.beans.factory.annotation.Qualifier

public @interface Qualifier

This annotation may be used on a field or parameter as a qualifier for candidate beans when autowiring. It may also be used to annotate other custom annotations that can then in turn be used as qualifiers.

@Qualifier用于注释一个字段或参数,当自动绑定时它作为候选bean的限定器。它也可以用于自定义的限定器注释。

属性

value

B.1、举例说明

我们使用@AutoWired按照类型自动绑定,当容器中有多个匹配的bean时,将绑定那一个呢?我们可以通过在需要绑定的字段或参数上使用@Qualifier(value=” bar1”)来指定要绑定的bean。并在bean定义中使用<bean id="bar1"<qualifier value="bar1"/>来关联。

范例1

1.Foo

Java代码

  1. public class Foo {   
  2. @Autowired
  3. @Qualifier(value="bar1") 
  4. private Bar bar; 

2.配置文件

配置文件中有2Bar,一个bean使用qualifier进行标注,一个使用id进行标注,容器将选择和@Qualifier(value)匹配的bean来进行绑定。

Xml代码

  1. <beans>
  2. <context:annotation-config/>
  3. <bean id="foo" class="x.y.Foo" />
  4. <bean class="x.y.Bar">
  5. <qualifier value="bar2"/>
  6. <property name="a" value="bar2" />
  7. </bean>
  8. <bean id="bar1" class="x.y.Bar">
  9. <property name="a" value="bar1" />
  10. </bean>
  11. </beans>

C、@Required

org.springframework.beans.factory.annotation.Required

public @interface Required

Marks a method (typically a JavaBean setter method) as being 'required': that is, the setter method must be configured to be dependency-injected with a value.

标注一个方法(通常是一个JavaBean的setter方法)是@Required,也就是,这个setter方法必须定义为通过一个值来依赖注入。

 

 

原文地址:http://lwg2001s.iteye.com/blog/1674039

[IoC]6 详解@Autowired、@Qualifier和@Required的更多相关文章

  1. Spring注解标签详解@Autowired @Qualifier等 @Slf4j

    @Slf4j @Slf4j注解实现日志输出 自己写日志的时候,肯定需要: private final Logger logger = LoggerFactory.getLogger(LoggerTes ...

  2. Spring注解标签详解@Autowired @Qualifier等

    http://blog.csdn.net/wangsr4java/article/details/42777855 @Component.@Repository.@Service.@Controlle ...

  3. [Spring学习笔记 1 ] Spring 简介,初步知识--Ioc容器详解 基本原理。

    一.Spring Ioc容器详解(1) 20131105 1.一切都是Bean Bean可是一个字符串或者是数字,一般是一些业务组件. 粒度一般比较粗. 2.Bean的名称 xml配置文件中,id属性 ...

  4. C#依赖注入控制反转IOC实现详解

    原文:C#依赖注入控制反转IOC实现详解 IOC的基本概念是:不创建对象,但是描述创建它们的方式.在代码中不直接与对象和服务连接,但在配置文件中描述哪一个组件需要哪一项服务.容器负责将这些联系在一起. ...

  5. 详解@Autowired、@Qualifier和@Required

    A.@Autowired org.springframework.beans.factory.annotation.Autowired public @interface Autowired Mark ...

  6. Spring IOC使用详解

    SpringIOC使用详解 一.IOC简介 IOC(Inversion of Control):控制反转,即对象创建的问题.通俗地讲就是把创建对象的代码交给了Spring的配置文件来进行的.这样做的优 ...

  7. IOC注解详解

    @Component 修改一个类,将这个类交给Spring管理 相当于在配置文件当中配置<bean id="" class=""> @Compone ...

  8. Spring IoC原理详解

    去掌握一门技术的时候,往往很多人都忽略了一点,只是一味地去写代码,原理层面的东西从来就不理会 还有就是学习的过程中,不去想为什么有了当前的写法,却有着这么一门技术可以代替它 一般来说,在写程序的时候, ...

  9. Spring IoC 使用详解

    在Spring中,依赖注入(DI)模式实现了控制反转(IoC)原理.让我们通过一个例子来帮助理解依赖注入.我们先看到java版的例子,然后在此基础上加上spring的功能.就例子而言,是相当地简单.Q ...

随机推荐

  1. Day1 summary

    对比了几篇在hadoop环境中实现关联规则.频繁项集的论文,文章结构都涉及mapreduce模型.传统与改进apriori算法比较.实验结果分析(数据规模-用时or加速比,节点-用时or加速比).有一 ...

  2. 深入分析:Android中app之间的交互(二,使用ComponentName)

    在前一篇相关主题的博文中我们了解了如何使用Action来启动当前应用之外的Activity处理我们的业务逻辑,在本篇笔记中我在简单介绍一下使用ComponentName来与当前应用之外的应用进行交互. ...

  3. 【转】关于iPhone界面适配详细版本

    对于上面哪一张适配图很多人不了解什么意思,现在我就慢慢地解释一下. 下面我们观看一下我们需要的几张效果图 3GS手机 iPhone 4/4S iPhone 5/5c/5s iPhone 6 iPhon ...

  4. iis6.0+.net 4.0 +mvc 404错误

    ps: 在iis中重新注册.net framework命令cd C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis.exe -i 1 ...

  5. Jquery autocomplete插件的使用

    简单用法: <%@ page language="java" contentType="text/html; charset=UTF-8" pageEnc ...

  6. 转:sql server索引碎片和解决方法

    毫无疑问,给表添加索引是有好处的,你要做的大部分工作就是维护索引,在数据更改期间索引可能产生碎片,所以一些维护是必要的.碎片可能是你查询产生性能问题的来源. 那么到底什么是索引碎片呢?索引碎片实际上有 ...

  7. href="#"与javascript:void(0)的区别

    共同点:都是一个空链接. 不同点:所以,#与javascript:void(0)的区别也很明显,#方法会跳转到页面的顶部,并且在页面URL后面会出现#,而javascript:void(0)方法不会, ...

  8. ZOJ 1067 Color Me Less

    原题链接 题目大意:一道类似于简单图像压缩的题目.给定一个调色板,然后把24位真彩色按照就近原则聚类. 解法:每个像素的色彩都是RGB三个值,相当于三维空间的一个点.所以当一个新的像素进来时,分别和调 ...

  9. Android EditText内容监听

    监听 EditText的内容变化,作出对应的处理. MainActivity.class package com.example.edittextdemo; import android.app.Ac ...

  10. sgu259 Printed PR    贪心

    link:http://acm.sgu.ru/problem.php?contest=0&problem=259 思路就是贪心. 首先要读懂题目,输入的方式,把样例读懂. 第一,打印的总时间一 ...