详解@Autowired、@Qualifier和@Required
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.配置文件
配置文件中有2个Bar,故这两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.配置文件
配置文件中有2个Bar,一个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方法必须定义为通过一个值来依赖注入。
详解@Autowired、@Qualifier和@Required的更多相关文章
- Spring注解标签详解@Autowired @Qualifier等 @Slf4j
@Slf4j @Slf4j注解实现日志输出 自己写日志的时候,肯定需要: private final Logger logger = LoggerFactory.getLogger(LoggerTes ...
- Spring注解标签详解@Autowired @Qualifier等
http://blog.csdn.net/wangsr4java/article/details/42777855 @Component.@Repository.@Service.@Controlle ...
- [IoC]6 详解@Autowired、@Qualifier和@Required
A.@Autowired org.springframework.beans.factory.annotation.Autowired public @interface Autowired Mark ...
- @Required @Autowired @Resource注解详解
一.@Required注解用于检查特定的属性是否设置 1.RequiredAnnotationBeanPostProcessor 为该注解的处理器,即bean后置处理器,检查所有带有该解的bean属性 ...
- @Autowired 注解详解
前言 我们平时使用 Spring 时,想要 依赖注入 时使用最多的是 @Autowired 注解了,本文主要讲解 Spring 是如何处理该注解并实现 依赖注入 的功能的. 正文 首先我们看一个测试用 ...
- Spring IoC @Autowired 注解详解
前言 本系列全部基于 Spring 5.2.2.BUILD-SNAPSHOT 版本.因为 Spring 整个体系太过于庞大,所以只会进行关键部分的源码解析. 我们平时使用 Spring 时,想要 依赖 ...
- 学习 Spring (九) 注解之 @Required, @Autowired, @Qualifier
Spring入门篇 学习笔记 @Required @Required 注解适用于 bean 属性的 setter 方法 这个注解仅仅表示,受影响的 bean 属性必须在配置时被填充,通过在 bean ...
- Rhythmk 一步一步学 JAVA (14) Spring-3 @Autowired,@Qualifier @Required @Resource @Component,@Service,@Controller,@Repository @PostConstruct,@PreDestroy
1.@Autowired 注解:首先在使用时候需要引入配置: <!-- 该 BeanPostProcessor 将自动起作用,对标注 @Autowired 的 Bean 进行自动注入 --> ...
- @Autowired 与@Resource的区别详解
spring不但支持自己定义的@Autowired注解,还支持几个由JSR-250规范定义的注解,它们分别是@Resource.@PostConstruct以及@PreDestroy. @Resour ...
随机推荐
- 怎么在手机浏览器上访问电脑本地的文件,局域网内,自建WiFi也可以
首先,电脑要有Mysql+Apache+PHP环境,我直接用Wampsever,开启环境后手机和电脑要再同一个局域网内,然后电脑上打开win+R,输入cmd,再输入ipconfig,就可以看着这台的电 ...
- nginx+fast-cgi+c
1. 下载fastcgi开发包,编译安装 http://www.fastcgi.com/dist/fcgi-current.tar.gz #wget http://www.fastcgi.com/di ...
- Day05_JAVA语言基础第五天
1.函数(掌握) 1.概念(掌握) 定义在类中,有特定功能的一小段程序 2.格式(掌握) 修饰符 返回类型 函数名(参数类型 形式参数,...){ 函数体: return 返回值; } 解释: A 修 ...
- div+css总结—FF下div不设置高度背景颜色或外边框不能正常显示的解决方法(借鉴)
原地址:http://blog.sina.com.cn/s/blog_60b35e830100qwr2.html 在使用div+css进行网页布局时,如果外部div有背景颜色或者边框,而不设置其高度, ...
- MySQL单表多字段模糊查询解决方法
例如现有table表,其中有title,tag,description三个字段,分别记录一条资料的标题,标签和介绍.然后根据用户输入的查询请求,将输入的字串通过空格分割为多个关键字,再在这三个字段中查 ...
- vs2013常用快捷键收集
vs2013快捷键: 复制一整行代码: Ctrl+C剪切一整行代码: Ctrl+X删除一整行代码: Ctrl+L跳转到指定的行:ctrl+G 注释:组合键“Ctrl+K+C”取消注释:组合键“Ctrl ...
- java 中的几种 "通用方法“
前言 Java中,除了基本的数值类型,其他所有数据类型(包括数组)都是对象. 而Object这个类是所有类的超类,它提供的方法,自然能够使用于它的所有子类(所有非基本数值类型). 本文介绍了Objec ...
- DEDE调用频道封面{dede:field:content/}内容方法
DEDE怎样在首页调用频道封面页{dede:field:content/}内容的方法,当我们用 织梦DEDECMS 做网站的时候,首页往往会加上关于我们或者企业简介之类的文字,在栏目里 当我们用织梦D ...
- Codeforces Round #308 (Div. 2) A B C 水 数学
A. Vanya and Table time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- 《C标准库》——之<stddef.h>
<stddef.h>,顾名思义,就是标准定义.C语言里这个标准库里定义了一些类型,和宏定义. <stddef.h>的内容: 类型: ptrdiff_t : 是两个指针相减的结果 ...