SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-004-消除bean自动装配的歧义@Primary
一、
假设有如下三个类实现同一个接口,则自动装配时会产生歧义
@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的更多相关文章
- 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, ...
- SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-007-给BEAN运行时注入值placeholder、@Value
一.用placeholder给bean运行时注入值的步骤 Spring取得placeholder的值是用${...} 1.声明placeholder bean (1)java方式 In order t ...
- SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-006-给bean运行时注入值(Environment,Property文件)
一. 直观的给bean注入值如下: @Bean public CompactDisc sgtPeppers() { return new BlankDisc( "Sgt. Pepper's ...
- 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 ...
- SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-002-激活PROFILE、设置默认值、@ActiveProfiles
一. Spring honors two separate properties when determining which profiles are active:spring.profiles. ...
- SPRING IN ACTION 第4版笔记-第三章Advancing wiring-001-DataSource在应用和开发环境之间切换 profile
一. DataSource在应用和开发环境的产生方式不同,可以用srping 的profile管理 Spring’s solution for environment-specific beans i ...
- SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-009-用SPEL给bean运行时注入依赖值
1.When injecting properties and constructor arguments on beans that are created via component-scanni ...
- 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 ...
- SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-003-@Conditional根据条件生成bean及处理profile
一.用@Conditional根据条件决定是否要注入bean 1. package com.habuma.restfun; public class MagicBean { } 2. package ...
随机推荐
- CF-gym-100523-C(水题)
Will It Stop? Available memory: 64 MB. Byteasar was wandering around the library of the University o ...
- 如何理解 Redux?
作者:Wang Namelos 链接:https://www.zhihu.com/question/41312576/answer/90782136 来源:知乎 著作权归作者所有,转载请联系作者获得授 ...
- count()与sum()
介绍Mysql中的count()与sum()区别 CREATE TABLE `result` ( `name` varchar(20) default NULL, `subject` varchar( ...
- php中怎么实现后台执行?
http://www.cnblogs.com/zdz8207/p/3765567.html php中实现后台执行的方法: ignore_user_abort(true); // 后台运行set_tim ...
- C++:memset ,memcpy 和strcpy 的根本区别!
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <assert.h&g ...
- 仿主题广告轮播js
function SlideShow(c) { var a = document.getElementById("slide"); var f = document.getElem ...
- 如何诊断oracle数据库运行缓慢或hang住的问题
为了诊断oracle运行缓慢的问题首先要决定收集哪些论断信息,可以采取下面的诊断方法:1.数据库运行缓慢这个问题是常见还是在特定时间出现如果数据库运行缓慢是一个常见的问题那么可以在问题出现的时候收集这 ...
- ubuntu 13.10自定义启动顺序
添加PPA sudo add-apt-repository ppa:danielrichter2007/grub-customizer sudo apt-get update sudo apt-get ...
- IOS 学习笔记 2015-03-24 OC-API-不可变字符串
大部分是模仿// // main.m // OC-API-不可变字符串 // // Created by wangtouwang on 15/3/25. // Copyright (c) 2015年 ...
- js在php 中出现 unterminated string literal 解决方法
出现这个问题就是空格造成的(可清空格符,换行符等) 示例代码如下: php 下报错 <?php echo "<a href=javascript:if(window.confir ...