首先解释一下@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引起的歧义现象的更多相关文章

  1. spring in action学习笔记十五:配置DispatcherServlet和ContextLoaderListener的几种方式。

    在spring in action中论述了:DispatcherServlet和ContextLoaderListener的关系,简言之就是DispatcherServlet是用于加载web层的组件的 ...

  2. spring in action 学习笔记十四:用纯注解的方式实现spring mvc

    在讲用纯注解的方式实现springmvc之前先介绍一个类:AbstractAnnotationDispatcherServletInitializer.这个类的作用是:任何一个类继承AbstractA ...

  3. spring in action学习笔记一:DI(Dependency Injection)依赖注入之CI(Constructor Injection)构造器注入

    一:这里先说一下DI(Dependency Injection)依赖注入有种表现形式:一种是CI(Constructor Injection)构造方法注入,另一种是SI(Set Injection) ...

  4. Spring in Action学习笔记(1)

    Spring基础 IoC 控制反转, 也称为DI-依赖注入 一.装配bean 推荐顺序:自动装配 -> JavaConfig装配 -> XML装配 1. 自动装配 @Component 注 ...

  5. Spring in Action 学习笔记三-AOP

    面向切面的Spring 2015年10月9日 11:30             屏幕剪辑的捕获时间: 2015-10-9 14:30             屏幕剪辑的捕获时间: 2015-10-9 ...

  6. Spring in Action 学习笔记二-DI

    装配bean 2015年10月9日 9:49             Sprng中,对象无需自己负责查找或创建其关联的其他对象.相关,容器负责吧需要相互协作的对象引用赋予各个对象. 创建应用对象之间协 ...

  7. Spring in Action 学习笔记一

    Spring 核心       Spring的主要特性仅仅是 依赖注入DI和面向切面编程AOP       JavaBean 1996.12 Javav 规范针对Java定义了软件组件模型,是简单的J ...

  8. spring in action学习笔记十六:配置数据源的几种方式

    第一种方式:JNDI的方式. 用xml配置的方式的代码如下: 1 <jee:jndi-lookup jndi-name="/jdbc/spittrDS" resource-r ...

  9. spring in action 学习笔记九:如何证明在scope为prototype时每次创建的对象不同。

    spring 中scope的值有四个:分别是:singleton.prototype.session.request.其中session和request是在web应用中的. 下面证明当scope为pr ...

随机推荐

  1. WinRT IO相关整理

    虽然一般UWP开发还是依赖.Net for UWP,但有时还是需要调用WinRT API.特别是在IO部分,WinRT有着和.Net似曾相识但又不尽相同的接口.在此对经常用到的一些地方进行一下整理. ...

  2. CF 24 D. Broken robot

    D. Broken robot 链接. 题意: 一个方格,从(x,y)出发,等价的概率向下,向左,向右,不动.如果在左右边缘上,那么等价的概率不动,向右/左,向下.走到最后一行即结束.求期望结束的步数 ...

  3. 6.Xilinx RapidIO核仿真与包时序分析

    转自https://www.cnblogs.com/liujinggang/p/10123498.html 一.软件平台与硬件平台 软件平台: 操作系统:Windows 8.1 64-bit 开发套件 ...

  4. java过滤器Filter笔记

    一.Filter简介 Filter也称之为过滤器,它是Servlet技术中最激动人心的技术之一,WEB开发人员通过Filter技术,对web服务器管理的所有web资源:例如Jsp,Servlet, 静 ...

  5. REST-framework快速构建API--认证

    一.API使用流程 使用过API的同学都知道,我们不可能任意调用人家的API,因为通过API可以获取很多关键数据,而且这个API可能供多个部门或个人使用,所以必须是经过授权的用户才能调用. API的使 ...

  6. Js_特效

    事件源对象 event.srcElement.tagName event.srcElement.type 捕获释放 event.srcElement.setCapture();  event.srcE ...

  7. Python3.7 + jupyter安装(CentOS6.5)

    Python3.7 + jupyter安装(CentOS6.5) 方法一(anaconda): anaconda是一个开源的Python发行版本 包含conda,python等大量的科学包以及依赖 优 ...

  8. PAT甲题题解-1107. Social Clusters (30)-PAT甲级真题(并查集)

    题意:有n个人,每个人有k个爱好,如果两个人有某个爱好相同,他们就处于同一个集合.问总共有多少个集合,以及每个集合有多少人,并按从大到小输出. 很明显,采用并查集.vis[k]标记爱好k第一次出现的人 ...

  9. linux 常用命令-用户、用户组管理

    注:本篇只涉及常用命令,全部命令可以通过help帮助查看. (1)type useradd   #查看命令属于内部命令还是外部命令,内部命令是嵌在linux的shell中,外部命令存储在路径中 (2) ...

  10. Docker 下 mysql 简单的 主从复制实现

    1. 拉取镜像 docker pull mysql: 2. 运行这个镜像 docker run -d --name maser mysql: 3. 安装一些必要的软件 docker exec -it ...