Spring 的@@Autowired 和 @Qualifier注释
@Autowired
spring2.1中允许用户通过@Autowired注解对Bean的属性变量.属性Setter方法以及构造方法进行标注,配合AutowiredAnnotationBeanProcessor完成Bean的自动配置。使用@Autowired注释进行byType注入。
为什么要引入@Autowired?因为通过 @Autowired的使用可以消除在xml中 set ,get方法的相关配置。也即是不用在xml进行相关的配置了。
1)当@Autowired使用在Bean的属性变量上时
public class TextEditor { @Autowired
private SpellChecker spellChecker; public TextEditor() {
System.out.println("在TextEditor的构造方法中");
} public SpellChecker getSpellChecker() {
return spellChecker;
}
}
如上代码,在TextEditor这个bean中对SepllChecker这个bean的属性变量使用了@Autowired注释,当扫描到TextEditor这个bean时会自动把SpellChecker这个bean注入进来。
2)当@Autowired注释使用在属性Setter方法上时:
@Autowired
public void setSpellChecker(SpellChecker spellChecker) {
this.spellChecker = spellChecker;
}
将 @Autowired 注释标注在 Setter 方法上,此时Setter的参数是某个Bean,当扫描到@AutoWired的时候就会将入参的Bean(SpellChecker)注入进来。
3)当@Autowired注释使用在构造方法上时:
@Autowired
public TextEditor(SpellChecker spellChecker){
System.out.println("Inside TextEditor constructor." );
this.spellChecker = spellChecker;
}
扫描到@AutoWired的时候就直接来看构造方法里面的入参是哪些bean,然后将对应的bean注入进来。
你知道,@Autowired
to autowire(or search) by-type
但是当不能确定 Spring 容器中一定拥有某个类的 Bean 时,可以在需要自动注入该类 Bean 的地方可以使用 @Autowired(required = false) ,这等于告诉 Spring:在找不到匹配 Bean 时也不报错。
@Qualifier 注释
上面的@Autowired注释是根据bean的类型起作用的,但是,如果出现了两个或者多个bean属于同一种类型,只用@Autowired就会出错,因为,它不知道你想注入的是哪个bean。此时就要引入@Qualifier注释加以区分
先看以下代码:
Student.java
package com.how2java.w3cschool.qualifier; public class Student {
private Integer age;
private String name; public Integer getAge() {
return age;
} public void setAge(Integer age) {
this.age = age;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} }
Profile.java
package com.how2java.w3cschool.qualifier; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier; public class Profile { @Autowired
@Qualifier("student1")
private Student student; public Profile() {
System.out.println("这是在Profile的构造方法中");
} public void printAge() {
System.out.println("Age:" + student.getAge());
} public void printName() {
System.out.println("Name:" + student.getName());
}
}
MainApp.java
package com.how2java.w3cschool.qualifier; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainApp { public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beanqualifier.xml");
Profile profile = (Profile)context.getBean("profile");
profile.printName();
profile.printAge(); } }
xml配置文件
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:annotation-config />
<bean id="profile"
class="com.how2java.w3cschool.qualifier.Profile">
</bean> <bean id="student1"
class="com.how2java.w3cschool.qualifier.Student">
<property name="name" value="章鱼哥" />
<property name="age" value="11" />
</bean> <bean id="student2"
class="com.how2java.w3cschool.qualifier.Student">
<property name="name" value="海绵宝宝" />
<property name="age" value="5" />
</bean> </beans>
你看,上面的xml文件中,是不是有两个bean student1和student2,它们属于同一类的?那么,此时如果使用@Autowired的话,就不知道用student1还是student2了,就要引入
@Autowired
@Qualifier("student1")
private Student student;
加以区分!
因此可以看到
@Qualifier
to autowire(or search) by-name
“ You can use @Qualifier
along with @Autowired
. In fact spring will ask you explicitly select the bean if ambiguous bean type are found, in which case you should provide the qualifier ”(stack overflow)
上述例子源于:W3Cschool,在此作记录
部分理解可能有误或者不到位,希望大家看到后可以不吝赐教(抱拳了)
Spring 的@@Autowired 和 @Qualifier注释的更多相关文章
- spring关于@Autowired和@Qualifier的使用
// package com.jhc.model; import org.springframework.stereotype.Component; @Component public interfa ...
- Spring中的注解 @Qualifier
在使用Spring框架中@Autowired标签时默认情况下使用 @Autowired 注释进行自动注入时,Spring 容器中匹配的候选 Bean 数目必须有且仅有一个.当找不到一个匹配的 Bean ...
- @Autowired 注释与@Qualifier 注释
@Service("OrganDaoIbatis") public class OrganDaoIbatis extends BaseDao implements IOrganDa ...
- Spring注解@Component、@Repository、@Service、@Controller @Resource、@Autowired、@Qualifier、@scope
以下内容摘自部分网友的,并加上了自己的理解 @Service用于标注业务层组件(我们通常定义的service层就用这个) @Controller用于标注控制层组件(如struts中的action.Sp ...
- Spring注解 @Component、@Repository、@Service、@Controller @Resource、@Autowired、@Qualifier 解析
@Repository.@Service.@Controller 这几个是一个类型,其实@Component 跟他们也是一个类型的 Spring 2.5 中除了提供 @Component 注释外,还定 ...
- Spring注解之@Autowired、@Qualifier、@Resource、@Value
前言 @Autowired.@Qualifier.@Resource.@Value四个注解都是用于注入数据的,他们的作用就和在xml配置文件中的bean标签中写一个标签的作用是一样的!本篇中特别要讲解 ...
- 关于Spring注解@Component、@Repository、@Service、@Controller @Resource、@Autowired、@Qualifier 解析
1.Spring 2.5 中除了提供 @Component 注释外,还定义了几个拥有特殊语义的注释,它们分别是:@Repository.@Service和 @Controller 其实这三个跟@Com ...
- Spring 注解注入—@Qualifier 注释
当创建多个具有相同类型的 bean 时,并且想要用一个属性只为它们其中的一个进行装配,在这种情况下,你可以使用 @Qualifier 注释和 @Autowired 注释通过指定哪一个真正的 bean ...
- Spring @Qualifier 注释
可能会有这样一种情况,当你创建多个具有相同类型的 bean 时,并且想要用一个属性只为它们其中的一个进行装配. 在这种情况下,你可以使用 @Qualifier 注释和 @Autowired 注释通过指 ...
随机推荐
- Spring Boot 2 (五):Docker Compose + Spring Boot + Nginx + Mysql 实践
Spring Boot 2 (五):Docker Compose + Spring Boot + Nginx + Mysql 实践 Spring Boot + Nginx + Mysql 是实际工作中 ...
- 怎样理解测试指标 :TPS和HPS
TPS(Transaction per second) 是估算应用系统性能的重要依据.其意义是应用系统每秒钟处理完成的交易数量. 一般的,评价系统性能均以每秒钟完成的技术交易的数量来衡量. 系统整体处 ...
- 应使用sqlplus代替tnsping进行oracle连通性测试
一直以来,都习惯于tnsping alias测试确定使用了那个sqlnet.ora,并测试连通性.最近在制作系统的安装包,为了轻量级以及提高实施效率,全部客户端使用oracle instant cli ...
- 【题解】Luogu P2157 [SDOI2009]学校食堂
原题传送门:P2157 [SDOI2009]学校食堂 一看题目就知道是状压dp 设f[i][j][k]表示第1到i-1个人都吃完了饭,第i个人以及后面的7个人是否打饭的状态为j,当前最后打饭的人的编号 ...
- (4opencv)如何基于GOCW,创建一个实时视频程序
直接使用提供的代码框架进行修改,是最快得到效果的方法:但是这样的灵活性较差,而且真正的程序员从来都不会停滞在这一步:我们需要的是"将框架解析到最小化.理清楚每个构建之间的关系",只 ...
- Delphi XE5 for Android (九)
Delphi XE5 下TEdit控件有个属性:KeyboardType,如下图: 该属性决定了当焦点进入TEdit时,系统弹出的输入窗体,按照其帮助文件说明,不同取值的输入窗体不同,如下图: 根 ...
- centos设置中文输入法无效的解决办法
安装 im-chooser: yum install im-chooser 回到当前使用的普通用户,设置 ibus 输入法为默认输入系统: imsettings-switch ibus
- Spyder 调出绘图界面
Tools->Preference->IPython console->Graphics->Graphics backend->QT4 or QT5
- POJ-1038 Bugs Integrated, Inc. (状压+滚动数组+深搜 的动态规划)
本题的题眼很明显,N (1 <= N <= 150), M (1 <= M <= 10),摆明了是想让你用状态压缩dp. 整个思路如下:由于要填2*3或者3*2的芯片,那么就要 ...
- P3810 【模板】三维偏序(陌上花开)(cdq分治)
思路 看到这种偏序类的题目,而且不要求强制在线,可以立刻想到cdq分治 注意这题有一个问题,就是询问的是小于等于而不是小于,如果相等的话两个元素会相互贡献,而cdq的特点是右区间不能对左边有影响,所以 ...