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 ...
随机推荐
- Servlet——提交表单信息,Servlet之间的跳转
HTML表单标签:<form></form> 属性: actoion: 提交到的地址,默认为当前页面 method: 表单提交方式 有get和post两种方式,默认为get ...
- 移动端页面滑动时候警告:Unable to preventDefault inside passive event listener due to target being treated as passive.
移动端项目中,在滚动的时候,会报出以下提示: [Intervention] Unable to preventDefault inside passive event listener due to ...
- SSISDB4:当前正在运行的Package及其Executable
SSISDB 系列随笔汇总: SSISDB1:使用SSISDB管理Package SSISDB2:SSIS工程的操作实例 SSISDB3:Package的执行实例 SSISDB4:当前正在运行的Pac ...
- fastDFS教程Ⅱ-文件服务器迁移
在实际的项目应用中,由于服务器替换或项目变更难免会存在fastDFS文件服务器迁移的工作.本文重点介绍fastDFS文件系统在不同情况下的文件迁移处理方案. 1.迁移时IP地址不变 通过文件服务器存储 ...
- github添加ssh连接用户
最近打算用flask写一个自己的博客网站,打算把代码放在GitHub上,使用ssh访问.记录下GitHub配置ssh用户的流程. 1.在本地电脑或云服务器上生成ssh公钥和私钥,window下可以进入 ...
- Unity 图文重现官方教程视频 2droguelike 第一集
初衷: 本人初学Unity,四处收集了一些视频和教材,学习和摸索了一段时间, 我发现官网教程简单易上手,只不过他是英文讲解不方便,我就想把他翻译翻译吧, 然后我又发现看视频学习要暂停回放好多遍,麻烦, ...
- DevOps on AWS之Cloudformation概念介绍篇
Cloudformation的相关概念 AWS cloudformation是一项典型的(IAC)基础架构即代码服务..通过编写模板对亚马逊云服务的资源进行调用和编排.借助cloudformation ...
- 机器学习初入门04 – Seaborn(持续更新)
Seaborn库可以说是在matplotlib库上的一个封装,它给我们提供了非常丰富的模板 一.整体布局风格设置 import seaborn as sns import numpy as np im ...
- linux 操作 mysql 指定端口登录 以及启动 停止
linux 操作 mysql 指定端口登录 mysql -uroot -p -h10.154.0.43 -P3341 1.查看mysql版本方法一:status;方法二:select version( ...
- linux 内核 第四周 扒开系统调用的三层皮 上
姬梦馨 原创作品 http://mooc.study.163.com/course/USTC-1000029000 一.用户态.内核态和中断处理过程 用户通过库函数与系统调用联系起来:库函数帮我们把系 ...