In Spring, @Qualifier means, which bean is qualify to autowired on a field. See following scenario :

Autowiring Example

See below example, it will autowired a “person” bean into customer’s person property.

package com.mkyong.common;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;public class Customer {@Autowiredprivate Person person;//...
}

But, two similar beans “com.mkyong.common.Person” are declared in bean configuration file. Will Spring know which person bean should autowired?

<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd"><bean
class ="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/><bean id="customer" class="com.mkyong.common.Customer" /><bean id="personA" class="com.mkyong.common.Person" ><property name="name" value="mkyongA" /></bean><bean id="personB" class="com.mkyong.common.Person" ><property name="name" value="mkyongB" /></bean></beans>

When you run above example, it hits below exception :

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.mkyong.common.Person] is defined: expected single matching bean but found 2: [personA, personB]

@Qualifier Example

To fix above problem, you need @Quanlifier to tell Spring about which bean should autowired.

package com.mkyong.common;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;public class Customer {@Autowired@Qualifier("personA")private Person person;//...
}

In this case, bean “personA” is autowired.

Customer [person=Person [name=mkyongA]]

转载于:https://www.cnblogs.com/ghgyj/p/4750257.html

Spring Autowiring @Qualifier example相关推荐

  1. Spring Auto-Wiring Beans

    In Spring framework, you can wire beans automatically with auto-wiring feature. To enable it, just d ...

  2. Spring的qualifier标签

    @Autowired是根据类型进行自动装配的.如果当Spring上下文中存在不止一个UserDao类型的bean时,就会抛出BeanCreationException异常;如果Spring上下文中不存 ...

  3. Spring注解@Qualifier的详细用法你知道几种「扩展点实战系列」- 第444篇

    历史文章(文章累计440+) <国内最全的Spring Boot系列之一> <国内最全的Spring Boot系列之二> <国内最全的Spring Boot系列之三> ...

  4. spring 配置项 qualifier小解

    qualifier 英文释义:限定词,修饰词: 1 spring设计这个bean的配置项,目的是限定装配的bean的确切名称:在xml配置文件中应该是bean的id值: 2 @Qualifier 注解 ...

  5. Spring 注解 @Qualifier 详细解析

    1. 概述 今天带你了解一下 Spring 框架中的 @Qualifier 注解,它解决了哪些问题,以及如何使用它.我们还将了解它与 @Primary 注解的不同之处. 2. 痛点 使用 @Autow ...

  6. Spring 注解 @Qualifier和@Primary 详细解析

    1. 概述 今天带你了解一下 Spring 框架中的 @Qualifier 注解,它解决了哪些问题,以及如何使用它.我们还将了解它与 @Primary 注解的不同之处. 2. 痛点 使用 @Autow ...

  7. Spring注解 @Qualifier 说明、用法

    前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家.点击跳转到教程. -------------------------------------------- 我是分隔线 ...

  8. spring中@Qualifier注解的使用

    <该注解可以使用在类上.属性上.参数上.方法上> 1.  当使用在类上的时候,如下代码,@Qualifier注解里面填写的值就是当前类注入到IOC容器的bean的唯一标识(id),再使用到 ...

  9. Spring学习(10)--- @Qualifier注解

    按类型自动装配可能多个bean实例的情况,可以使用Spring的@Qualifier注解缩小范围(或指定唯一),也可以指定单独的构造器参数或方法参数 可用于注解集合类型变量 例子: package c ...

最新文章

  1. logback.xml配置
  2. BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [
  3. dubbo使用遇到的问题
  4. c语言冒泡法加逗号,Ubuntu 10.04 LTS 无法添加计算机的问题
  5. css overflow属性的测试
  6. uiuc计算机课程,UIUC计算机科学专业解读
  7. 工业级光纤收发器一般能正常使用多长时间?
  8. mysql 5.5 主从同步问题_MySQL 5.5 主从复制异步、半同步以及注意事项详解
  9. linux中printf命令,Linux中printf命令使用实例
  10. 两个网站:借物网,rss交流网站opml.cn
  11. 八大算法思想(二)------------------递归算法
  12. linux scp ssh拷贝文件,linux-远程拷贝文件之scp
  13. 微软MED-V虚拟化实战教程之二映像准备和测试
  14. win7计算机资源管理器卡住,Win7资源管理器未响应的解决方法
  15. python机器学习实战 getA()函数详解
  16. GPL和MIT开源协议
  17. 双系统Linux grub引导,双系统修复ubuntu grub2引导的方法
  18. Objective-C基础教程读书笔记(3)
  19. NYU计算机系,NYU的Computer Science and Engineering「纽约大学计算机科学与工程系」
  20. lcs算法c语言代码,动态规划算法-LCS

热门文章

  1. 任丽萍跟我赌200块钱的
  2. openstack云计算实践-老男孩架构师课程教案笔记分享
  3. Mac OSX 下如何显示隐藏文件
  4. Android多渠道打包APK
  5. Java项目性能监控和调优工具-Javamelody
  6. Fragment试手
  7. 设计模式学习之---Facade(外观)模式
  8. [译] 在远程工作中领悟到的 10 件事
  9. antd下拉框联动说明
  10. 20.1 shell脚本介绍 20.2 shell脚本结构和执行 20.3 date命令用法 20.4 shell脚本中的变量...