首先解释一下@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. 【Win32 API】利用SendMessage实现winform与wpf之间的消息传递

    原文:[Win32 API]利用SendMessage实现winform与wpf之间的消息传递 引言    有一次心血来潮,突然想研究一下进程间的通信,能够实现消息传递的方法有几种,其中win32ap ...

  2. Vue 使用细节收集

    JSX 中 on 开头的属性名 在用 elementui 中的 el-upload 的时候,他们组件中有一个属性 on-change ,也不知道谁想出来的属性名,太扯淡了,非要 on 开头,我开始的代 ...

  3. Linux 平台和 Windows平台下 Unicode与UTF-8互转

    Windows: unsigned char * make_utf8_string(const wchar_t *unicode) { , index = , out_index = ; unsign ...

  4. [CF995F]Cowmpany Cowmpensation[树形dp+拉格朗日插值]

    题意 给你一棵树,你要用不超过 \(D\) 的权值给每个节点赋值,保证一个点的权值不小于其子节点,问有多少种合法的方案. \(n\leq 3000, D\leq 10^9\) 分析 如果 \(D\) ...

  5. Markdown之语法入门篇

    Markdown语法入门 一.什么是Markdown语言 我相信有很多小伙伴没有听说过Markdown语言.的确,对于一般人来说,有word足够了.但是有这么一群人,受够了word那糟糕的排版方式,需 ...

  6. 关于T/G/M/K

    //扫盲贴 K, G, T,都是表数量,只是个数字,在不同的场合下表示的不同.在计算机行业中,这几个量可用来表示数据传输速度和容量,下面分别讨论,希望不了解的朋友不要被某知道上的误解了.如果有什么错误 ...

  7. 团队作业week7

    软件分析和用户需求调查 具体细则见: http://www.cnblogs.com/xinz/p/3308608.html

  8. Actual Time Cost

  9. 第三周 构造一个简单的Linux系统MenuOS

    一.   Linux内核源代码简介 稳定版内核:Linux-3.18.6 Linux内核源代码的目录结构: arch目录:在Linux内核源代码里占有的比重很大,因为Linux内核支持很多的体系结构, ...

  10. 20135323符运锦----第三周:构建一个简单的Linux系统MenuOS

    相关知识点 1.arch目录 占据相当庞大的空间,X86目录下代码需要重点关注. 2.init目录 内核启动的相关代码基本都在此目录下,内含MAIN.C,文件中START_KERNEL是整个LINUX ...