spring in action 学习笔记八:用@Primary 或者@Qualifier消除@Autowired引起的歧义现象
首先解释一下@Primary和@Qualifier这两个注解的意思:@Primary的意思是在众多相同的bean中,优先使用用@Primary注解的bean.而@Qualifier这个注解则指定某个bean有没有资格进行注入。
示例代码的思路是:1.一个接口Dessert和这个接口的三个实现类,2.再在一个类(AbrahamLincoln)中自动注入Dessert这个接口,3.用自动扫描机制自动创建bean.
如果不用@Primary和@Qualifier注解,势必出现如下错误:NoUniqueBeanDefinitionException.
示例代码如下:【用@Primary来解决问题】
Dessert接口的代码如下:
package com.advancedWiring.ambiguityInAutowiring; import org.springframework.stereotype.Component; /**
* Created by ${秦林森} on 2017/6/9.
*/
@Component
public interface Dessert {
void amI();
}
Cake的代码如下:
package com.advancedWiring.ambiguityInAutowiring; import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component; /**
* Created by ${秦林森} on 2017/6/9.
*/
@Component
public class Cake implements Dessert {
@Override
public void amI() {
System.out.println("I am cake");
}
}
Cookie的代码如下:
package com.advancedWiring.ambiguityInAutowiring; import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component; /**
* Created by ${秦林森} on 2017/6/9.
*/
@Component
public class Cookie implements Dessert {
@Override
public void amI() {
System.out.println("I am cookie");
}
}
IceCream的代码如下:
package com.advancedWiring.ambiguityInAutowiring; import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component; /**
* Created by ${秦林森} on 2017/6/9.
*/
@Component
@Primary//注意这里用了@Primary这个注解。
public class IceCream implements Dessert{
@Override
public void amI() {
System.out.println("I am ice cream");
}
}
DessertConfig的代码如下:
package com.advancedWiring.ambiguityInAutowiring; import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; /**
* Created by ${秦林森} on 2017/6/9.
*/
@Configuration
/**
* 用@ComponentScan自动扫描创建bean
*/
@ComponentScan(basePackageClasses = Dessert.class)
public class DessertConfig { }
测试类的代码如下:
package com.advancedWiring.ambiguityInAutowiring; import org.springframework.context.annotation.AnnotationConfigApplicationContext; /**
* Created by ${秦林森} on 2017/6/9.
*/
public class AmbiguityTest {
public static void main(String[] args) {
AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(DessertConfig.class);
Dessert dessert = ac.getBean(Dessert.class);
dessert.amI();
}
}
二:用@Qualifier这个注解来解决问题:
核心代码如下:
package com.advancedWiring.ambiguityInAutowiring; import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component; /**
* Created by ${秦林森} on 2017/6/9.
*/
@Component
@Qualifier("crispy")
public class Cookie implements Dessert {
@Override
public void amI() {
System.out.println("I am cookie");
}
}
package com.advancedWiring.ambiguityInAutowiring; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component; /**
* Created by ${秦林森} on 2017/6/9.
*/
@Component
public class AbrahamLincoln {
private Dessert dessert; public Dessert getDessert() {
return dessert;
}
@Autowired
@Qualifier("crispy")
public void setDessert(Dessert dessert) {
this.dessert = dessert;
}
}
spring in action 学习笔记八:用@Primary 或者@Qualifier消除@Autowired引起的歧义现象的更多相关文章
- spring in action学习笔记十五:配置DispatcherServlet和ContextLoaderListener的几种方式。
在spring in action中论述了:DispatcherServlet和ContextLoaderListener的关系,简言之就是DispatcherServlet是用于加载web层的组件的 ...
- spring in action 学习笔记十四:用纯注解的方式实现spring mvc
在讲用纯注解的方式实现springmvc之前先介绍一个类:AbstractAnnotationDispatcherServletInitializer.这个类的作用是:任何一个类继承AbstractA ...
- spring in action学习笔记一:DI(Dependency Injection)依赖注入之CI(Constructor Injection)构造器注入
一:这里先说一下DI(Dependency Injection)依赖注入有种表现形式:一种是CI(Constructor Injection)构造方法注入,另一种是SI(Set Injection) ...
- Spring in Action学习笔记(1)
Spring基础 IoC 控制反转, 也称为DI-依赖注入 一.装配bean 推荐顺序:自动装配 -> JavaConfig装配 -> XML装配 1. 自动装配 @Component 注 ...
- Spring in Action 学习笔记三-AOP
面向切面的Spring 2015年10月9日 11:30 屏幕剪辑的捕获时间: 2015-10-9 14:30 屏幕剪辑的捕获时间: 2015-10-9 ...
- Spring in Action 学习笔记二-DI
装配bean 2015年10月9日 9:49 Sprng中,对象无需自己负责查找或创建其关联的其他对象.相关,容器负责吧需要相互协作的对象引用赋予各个对象. 创建应用对象之间协 ...
- Spring in Action 学习笔记一
Spring 核心 Spring的主要特性仅仅是 依赖注入DI和面向切面编程AOP JavaBean 1996.12 Javav 规范针对Java定义了软件组件模型,是简单的J ...
- spring in action学习笔记十六:配置数据源的几种方式
第一种方式:JNDI的方式. 用xml配置的方式的代码如下: 1 <jee:jndi-lookup jndi-name="/jdbc/spittrDS" resource-r ...
- spring in action 学习笔记九:如何证明在scope为prototype时每次创建的对象不同。
spring 中scope的值有四个:分别是:singleton.prototype.session.request.其中session和request是在web应用中的. 下面证明当scope为pr ...
随机推荐
- EZ 2018 02 28 NOIP2018 模拟赛(二)
我TM的终于改完了(其实都是SB题) 题目链接:http://211.140.156.254:2333/contest/53 T1送分,T2前40%送分,还有骗分机制在里面,T3暴力50 所以200应 ...
- [CF1062F]Upgrading Cities[拓扑排序]
题意 一张 \(n\) 点 \(m\) 边的 \(DAG\) ,问有多少个点满足最多存在一个点不能够到它或者它不能到. \(n,m\leq 3\times 10^5\) 分析 考虑拓扑排序,如果 \( ...
- stl源码剖析 详细学习笔记 hashset hashmap
//---------------------------15/03/26---------------------------- //hash_set { /* hash_set概述: 1:这是一个 ...
- 从头到尾谈一下HTTPS
引言 “你能谈一下HTTPS吗?” “一种比HTTP安全的协议.” “...” 如果面试这样说的话那差不多就gg了,其实HTTPS要展开回答的话内容还挺丰富的.本篇文章详细介绍了HTTPS是什么.为什 ...
- Python中 list, numpy.array, torch.Tensor 格式相互转化
1.1 list 转 numpy ndarray = np.array(list) 1.2 numpy 转 list list = ndarray.tolist() 2.1 list 转 torch. ...
- 实验吧CTF天网管理系统
天网你敢来挑战嘛 格式:ctf{ } 解题链接: http://ctf5.shiyanbar.com/10/web1/ 打开链接后,嗯,光明正大的放出账号密码,肯定是登不进的,查看源代码 看来是和md ...
- PAT甲题题解-1043. Is It a Binary Search Tree (25)-二叉搜索树
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6789220.html特别不喜欢那些随便转载别人的原创文章又不给 ...
- one team
Double H Team 1.队员 王熙航211606379(队长) 李冠锐211606364 曾磊鑫211606350 戴俊涵211606359 聂寒冰211606324 杨艺勇211606342 ...
- HDU 2015 偶数求和
http://acm.hdu.edu.cn/showproblem.php?pid=2015 Problem Description 有一个长度为n(n<=100)的数列,该数列定义为从2开始的 ...
- Activiti随着Spring启动自动部署开关
Activiti的act_re_deployment表NAME_列:全部显示SpringAutoDeployment. 查阅Activiti,https://github.com/Activiti/A ...