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

对数组使用自动绑定

public class Foo {    @Autowired  private Bar[] bars;
}  

2.配置文件

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

<beans>  <context:annotation-config/>  <bean id="foo" class="x.y.Foo" />  <bean id="bar1" class="x.y.Bar">  <property name="a" value="bar1" />  </bean>  <bean id="bar2" class="x.y.Bar">  <property name="a" value="bar2" />  </bean>
</beans>  

3.测试类

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

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

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

Set<Bar>

Map<String,Bar>

A.2、required

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

@Autowired(required=false)
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

public class Foo {    @Autowired  @Qualifier(value="bar1")  private Bar bar;
}  

2.配置文件

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

<beans>  <context:annotation-config/>  <bean id="foo" class="x.y.Foo" />  <bean class="x.y.Bar">  <qualifier value="bar2"/>  <property name="a" value="bar2" />  </bean>  <bean id="bar1" class="x.y.Bar">  <property name="a" value="bar1" />  </bean>
</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方法必须定义为通过一个值来依赖注入。

转载于:https://www.cnblogs.com/linjian/p/4651441.html

详解@Autowired、@Qualifier和@Required相关推荐

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

    @Autowired spring2.1中允许用户通过@Autowired注解对Bean的属性变量.属性Setter方法以及构造函数进行标注,配合AutowiredAnnotationBeanProc ...

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

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

  3. @Autowired详解

    @Autowired详解 @Autowired是在Spring中新引入的注解,所属的包为org.springframework.beans.factory.annotation; 定义如下: @Tar ...

  4. springMVC注解中@RequestMapping中常用参数value params 以及@RequestParam 详解

    转载自 https://blog.csdn.net/qq_35067322/article/details/52811300?locationNum=9&fps=1 https://www.c ...

  5. authorizationPolicy详解

    欢迎关注我的公众号: 目前刚开始写一个月,一共写了18篇原创文章,文章目录如下: istio多集群探秘,部署了50次多集群后我得出的结论 istio多集群链路追踪,附实操视频 istio防故障利器,你 ...

  6. @Resource,@Autowired,@Inject3种注入方式详解

    转载自 @Resource,@Autowired,@Inject3种注入方式详解 概况 @Resource,@Autowired,@Inject 这3种都是用来注入bean的,它们属于不同的程序中. ...

  7. @Autowired注解详解——超详细易懂

    @Autowired详解 要搞明白@Autowired注解就是要了解它是什么?有什么作用?怎么用?为什么? 首先了解一下IOC操作Bean管理,bean管理是指(1)spring创建对象 (2)spr ...

  8. java shareable_spring中@Resource和@Autowired理解详解_编程语言_IT虾米网

    @Resource在bean注入的时候使用,@Resource所属包其实不是spring,而是javax.annotation.Resource,只不过spring支持该注解 @Resource里有n ...

  9. @Autowired用法详解

    @Autowired 注解简介 @Autowired 注解,它可以对类成员变量.方法及构造函数进行标注,完成自动装配的工作. 通过 @Autowired的使用来消除 set ,get方法.在使用@Au ...

最新文章

  1. 多线程导出大规模excel文件
  2. [Google Guava] 8-区间
  3. 数据科学可视化之要途
  4. acl在内核里的位置_在Linux中使用ACL(访问控制列表)保护文件/目录
  5. 阿里、网易、滴滴共十次前端面试碰到的问题
  6. java redis监听问题_springboot+redis过期事件监听实现过程解析
  7. C# 自定义sqlserver表值函数
  8. 十分钟一起学会ResNet残差网络
  9. Scapy:send函数剖析(参数、返回值、应用)
  10. VBS 对IBM Notes的常规操作
  11. 访问chm文件出现 已取消到该网页的导航的解决方法
  12. e01文件镜像SHA1值
  13. Kotlin里的takeIf和takeUnless
  14. 项目管理心得体会(一)
  15. Java 高并发第二阶段实战---高并发设计模式,内存模型,CPU一致性协议,volatile关键字剖析
  16. 道理我都懂,但是这种列车为什么会自己摆动?
  17. python gui包_超酷 Python 程序包 ,一行代码搭建 GUI 界面
  18. css30主要作用,30个你不可不知的CSS选择器小结
  19. JDK简介及安装配置|史上最全详细版
  20. C语言学习笔记10-指针(动态内存分配malloc/calloc、realloc、释放free,可变数组实现;Tips:返回指针的函数使用本地变量有风险!;最后:函数指针)

热门文章

  1. 近视手术─医学界的一个阴谋? !
  2. 通过显示当前 python 程序占用的内存大小来比较生成器和迭代器(转载)
  3. Springboot版本+ Spring Framework版本 + jdk版本 + Maven版本
  4. Lambda expressions are not supported at language level ‘5‘
  5. 0009:err:module:__wine_process_init failed to load xxx
  6. Cython屏蔽GIL锁实践
  7. django-oscar相关的模块调研信息汇总
  8. xfce开始菜单增加一个新的图标
  9. trust cv的含义
  10. 文档和词项之间的相关度计算汇总