一、

假设有如下三个类实现同一个接口,则自动装配时会产生歧义

 @Component
public class Cake implements Dessert { ... }
@Component
public class Cookies implements Dessert { ... }
@Component
public class IceCream implements Dessert { ... } @Autowired
public void setDessert(Dessert dessert) {
this.dessert = dessert;
}

二、@Primary的3种用法

Let’s say that ice cream is your favorite dessert. You can express that favorite choice
in Spring using the @Primary annotation. @Primary can be used either alongside
@Component for beans that are component-scanned or alongside @Bean for beans
declared in Java configuration. For example, here’s how you might declare the
@Component -annotated IceCream bean as the primary choice:

1.在自动扫描

@Component
@Primary
public class IceCream implements Dessert { ... }

2.在java配置文件中

@Bean
@Primary
public Dessert iceCream() {
return new IceCream();
}

3.xml配置文件

 <bean id="iceCream"
class="com.desserteater.IceCream"
primary="true" />

如果有两个合适的bean都标记为@Primary,则Spring还是无法确定要装配哪个bean,这时要用@Qulifier来解决歧义

SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-004-消除bean自动装配的歧义@Primary的更多相关文章

  1. SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-008-SpEL介绍

    一. 1.SpEL expressions are framed with  #{ ... } 2.SpEl的作用 Sp EL has a lot of tricks up its sleeves, ...

  2. SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-007-给BEAN运行时注入值placeholder、@Value

    一.用placeholder给bean运行时注入值的步骤 Spring取得placeholder的值是用${...} 1.声明placeholder bean (1)java方式 In order t ...

  3. SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-006-给bean运行时注入值(Environment,Property文件)

    一. 直观的给bean注入值如下: @Bean public CompactDisc sgtPeppers() { return new BlankDisc( "Sgt. Pepper's ...

  4. SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-005-Bean的作用域@Scope、ProxyMode

    一. Spring的bean默认是单例的 But sometimes you may find yourself working with a mutable class that does main ...

  5. SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-002-激活PROFILE、设置默认值、@ActiveProfiles

    一. Spring honors two separate properties when determining which profiles are active:spring.profiles. ...

  6. SPRING IN ACTION 第4版笔记-第三章Advancing wiring-001-DataSource在应用和开发环境之间切换 profile

    一. DataSource在应用和开发环境的产生方式不同,可以用srping 的profile管理 Spring’s solution for environment-specific beans i ...

  7. SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-009-用SPEL给bean运行时注入依赖值

    1.When injecting properties and constructor arguments on beans that are created via component-scanni ...

  8. SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-004-消除BEAN自动装配的歧义@QUALIFIER及自定义注解

    一. The @Qualifier annotation is the main way to work with qualifiers. It can beapplied alongside @Au ...

  9. SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-003-@Conditional根据条件生成bean及处理profile

    一.用@Conditional根据条件决定是否要注入bean 1. package com.habuma.restfun; public class MagicBean { } 2. package ...

随机推荐

  1. how to forget about delta cycles for RTL design

    A delta cycle is a VHDL construct used to makeVHDL, a concurrent language, executable on asequential ...

  2. react 资源汇总

    前端变化虽快,但其实一直都围绕这几个概念在转: URL - 访问什么页面 Data - 显示什么信息 View - 页面长成什么样 Action - 对页面做了什么操作 API Server - Da ...

  3. C# HTTP 请求

    public class HttpHelper { /// <summary> /// 创建GET方式的HTTP请求 /// </summary> public static ...

  4. WCF入门及在WinForm中动态调用

    一.WCF入门 1. 新建立空白解决方案,并在解决方案中新建项目,项目类型为:WCF服务应用程序,删除系统生成的两个文件IService1.cs与Service1.svc, 添加自定义的WCF[服务文 ...

  5. Druid 简单介绍

    官方网址:http://code.alibabatech.com/wiki/display/Druid/Home 1.什么是Druid Druid首先是一个数据库连接池.Druid是目前最好的数据库连 ...

  6. iframe框根据内容自适应高度

    1.页面 <iframe name="iframe_userCenter" id="iframe" frameborder=2 width=100% he ...

  7. (jQuery 插件)封装容器的表单为json对象

    下面代码可以把一个页面容器中的表单元素封装成一个json对象. (function($){ $.fn.serializeObject=function(){ var inputs=$(this).fi ...

  8. object 属性 对象的继承 (原型, call,apply)

    object 为一切对象的基类! 属性:constructor: 对创建对象的函数的引用Prototype: 原型(类型) hasOwnProperty(property):判断对象是否有某个特定的属 ...

  9. angularjs制作的iframe后台管理页切换页面

    <code> <!DOCTYPE html><html lang="zh" ng-app><head> <meta chars ...

  10. 大量字段表单在PHP便捷处理分享

    关于程序开发中的表单批量提交策略很多时候一个表单太多的字段,如何能够高效获取表单字段,也为如何提神开发的效率和统一性? 比如一个系统的某个有26个字段,那么我用表单的名称用26个a到z的字母, 你是选 ...