Deal with the ambiguity of automatic assembly(处理自动装配的歧义问题)

It’s first time for me to use this platform.And as u see,I choose to write this in English. So many reason,mostly because I like it.

Ok,time to get into business.
When first met this problem,When the Spring told me :

NoUniqueBeanDefinitionException:
nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException:
No qualifying bean of type [********] is defined:
expected single matching bean but found X:.....

“Do i know you?” Puzzled. All I remember is I used much time to figure anwser out.Looked for information about it on the internet and asked my friends. Of course got what I want, but soon forgot. Can’t find a better method, i bought 《Spring IN ACTION》and read it about 3 times(bad memory?NO! Just because write them down in my notebook,and only write down in notebook…so, I wanna tell you DO NOT RELY TOO MUCH ON NOTEBOOK BUT YOUR MIND).
Ok,time to get into business again.
HOW TO DEAL WITH IT?
Ⅰ、Mark the preferred bean
What it means?Like u like to fruit,among thousands of fruit,which one do u like best?Watermelons?Pitaya?Juicy peach?Grapes…Make a mark to tell me.
Just tell Spring which bean you need in this way.Now u need to use @Primary.

@Conponent
@Primary
public class classNae implements interface{....}

But make sure there is only one thing u like best or u want more problems.
Ⅱ、Qualify the auto-assembled bean ----- qualifiers
Method Ⅰ got A big problem : Its limitation, ProMonkey can only make one choice.In opposite terms,qualifiers can let monkeys have many choices.
@Qualifier is the main way to use qualifiers.It can work with @Autowired.

@Autowired
@Qualifier("beanID")
public void methodName(parameters){Method block
}

It’s a simple example.Remember one thing:beanID is a class name start with lowercase.If you change the class name, do not forget to change the qualifier.
Ⅲ、Create a custom qualifier
We can create a unique qualifier for a class when we create a class.

@Component
@Qualifier("specificName")
public class className implements interface{....}

The advantage of this method is that there is no coupling. It means if you change the class name and you forget to change qualifier, it’s ok, still works.

Ⅳ、Use custom qualifier annotations
There is one thing you need to know:Java does not allow multiple annotations of the same type to be repeated on the same entry.Like:

   @Autowired//beanID1 and beanID2 are same type@Qualifier("beanID1")@Qualifier("beanID2")public void methodName(parameters){Method block}

If you do that,compiler will tell you : “error!”.And the problem is what if we cannot find a way to choose the specific-bean in a group because there are many bean with same properties,what do we do?
Yes,use custom qualifier annotations.
HOW?

@Target({ElementType.CONSTRUCTOR,ElementType.FIELD,ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Qualifier
public @interface className{ }

Now you can use a custom qualifier annotations : @className

If there are any shortcomings, please point them out,thanks!

Spring_1_Deal with the ambiguity of automatic assembly(处理自动装配的歧义问题)相关推荐

  1. SqlServer 2016新特性 —— automatic seeding (自动种子设定)究竟是什么

    在 SQL Server 2012 和 2014 中,初始化 SQL Server Always On 可用性组中的次要副本的唯一方法是手动执行备份.复制和还原.SQL Server 2016 引入了 ...

  2. Automatic Plugin -WordPress自动采集插件 v3.56.0

    WordPress Automatic Plugin自动采集有针对性的高质量的文章文章,比如 亚马逊产品. Clickbank 产品. Youtube 视频. Vimeo 视频. Feeds post ...

  3. Spring之IOC自动装配

    7.Bean的自动装配 自动装配是Spring满足bean依赖一种方式! Spring会在上下文中自动寻找,并自动给bean装配属性! 在spring中有三种装配的方式 在xml中显示的配置 在jav ...

  4. Oracle 19c 新特性:自动化索引 Automatic indexing 实践

    老张拉呱:thomas zhang,甲骨文云平台事业部资深技术顾问,2008年加入甲骨文公司数据库咨询部门,10+年甲骨文解决方案咨询支持经验,资深系统工程师.Oracle OCM认证专家,具有丰富的 ...

  5. C#Assembly详解

    Assembly(翻译成中文是集会), 这里把它翻译为配件或程序集, 以示和组件(Component)加以区别.一个配件有时候是指一个EXE或者DLL文件, 实际上是一个应用程序(就是指带有主程序入口 ...

  6. Oracle 19c 新特性 —— 自动索引 Automatic indexing

    自动索引功能可自动执行Oracle数据库中的索引管理任务.根据应用程序工作负载的变化自动创建.重建和删除数据库中的索引,从而提高数据库性能. Automatic indexing特性对于on-prem ...

  7. PyTorch随笔 - Automatic Differentiation 自动微分

    面试题: 函数:f(1) = x, f(n+1) = 4*f(n)*(1-f(n)) 实现f(4)的函数,当x=2时,f(4)的值是多少,导数(微分)值是多少? 即: def func(x, k):. ...

  8. 【SystemVerilog基础】program块的automatic与static深入探究

    文章目录 1.自动存储与静态存储介绍 2.任务定义为automatic的方法 3.Program的优势 4.Program的功能 参考 为避免仿真和设计竞争问题(race condition),sys ...

  9. 【综述笔记】 A survey of automatic generation of source code comments Algorithms and techniques

    A survey of automatic generation of source code comments Algorithms and techniques ABSTRACT 代码注释存在问题 ...

最新文章

  1. bootstrap 横铺 行_Bootstrap 排版
  2. 安装 node-sass 时报错
  3. Postgres-XL的使用与动态增删数据节点
  4. 数据库SQL Server 2019安装向导的“功能选择”详细说明(微软官方资料)
  5. 思科认证36个热门考点汇总
  6. php post授权编写,php模拟post行为代码总结(POST方式不是绝对安全)
  7. 台式计算机如何连接vif,关于计算机二级( VIF ) 县城有高手吗? 急寻
  8. 【Pytorch神经网络理论篇】 37 常用文本处理工具:spaCy库+torchtext库
  9. oracle基本的操作命令,oracle命令基本操作
  10. 计算机办公软件应用二级 考试题库,计算机二级办公软件高级应用技术考试真题题库...
  11. 为什么很多人转行学习Web前端技术?
  12. linux怎么用网络yum源,Linux配置本地网络YUM源
  13. 测试开发新手:从0到1开展性能测试必备的性能测试要点!
  14. 如何让Zen Cart 不在头部显示分类
  15. 【陆勤阅读】参加kaggle数据挖掘竞赛是怎样一种体验?
  16. JDK1.8后List转Map
  17. 桌面计算机密码修改频率,教大家如何更改电脑的显示频率
  18. da2 Android版本固件,OPPOAce2官方系统刷机包(完整固件最新升级包ColorOS 7)
  19. Qtdesigner设计实例——计算器 +可执行文件exe制作
  20. MySQL的多表关联查询

热门文章

  1. SAP采购订单中项目类别B(限制)的控制逻辑
  2. 国产AD芯片 MS1243应用
  3. android蓝牙投屏连接不上,乐播投屏常见使用问题汇总
  4. 违反唯一约束条件oracle那行报错,没有重复却提示:ORA-00001: 违反唯一约束条件...
  5. Android驱动开发
  6. 智能电表中的篡改检测与保护
  7. 【QT编译错误】Error while building/deploying project
  8. python编写网络防火墙怎么设置_分析目录下所有Eudemon防火墙配置的Python脚本
  9. ssh-keygen生成密钥对
  10. AndroidStudio的几个个性的配置和个人笔记