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 { @Autowired
private 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/beans
http://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]]

Spring Autowiring @Qualifier example的更多相关文章

  1. Spring Auto-Wiring Beans with @Autowired annotation

    In last Spring auto-wiring in XML example, it will autowired the matched property of any bean in cur ...

  2. Spring Autowiring by AutoDetect

    In Spring, "Autowiring by AutoDetect", means chooses "autowire by constructor" i ...

  3. Spring Autowiring by Constructor

    In Spring, "Autowiring by Constructor" is actually autowiring by Type in constructor argum ...

  4. Spring Autowiring by Name

    In Spring, "Autowiring by Name" means, if the name of a bean is same as the name of other ...

  5. Spring Autowiring by Type

    In Spring, "Autowiring by Type" means, if data type of a bean is compatible with the data ...

  6. Spring Auto-Wiring Beans

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

  7. Spring的qualifier标签

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

  8. Spring注解@Qualifier

    在使用Spring框架中@Autowired标签时默认情况下使用 @Autowired 注释进行自动注入时,Spring 容器中匹配的候选 Bean 数目必须有且仅有一个.当找不到一个匹配的 Bean ...

  9. 【spring】@Qualifier注解

    近期在捯饬spring的注解,现将遇到的问题记录下来,以供遇到同样问题的童鞋解决~ 先说明下场景,代码如下: 有如下接口: public interface EmployeeService { pub ...

随机推荐

  1. maven - setting.xml

    <?xml version="1.0" encoding="UTF-8"?> <!-- Licensed to the Apache Soft ...

  2. 如何过滤 adb logcat 输出

    对原作者表示感谢,转自博客:http://www.otechu.me/zh/2011/12/filtering-adb-logcat-output/ 本文介绍如何在 shell 命令行中过滤 adb ...

  3. Jenkins User on Apt-Get Install Installation

    转自:http://stackoverflow.com/questions/6234016/jenkins-user-on-apt-get-install-installation QUESTION: ...

  4. Codeforces 379D - New Year Letter

    原题地址:http://codeforces.com/contest/379/problem/D 题目大意:给出一种生成字符串的方法 s[k] = s[k - 2] + s[k - 1],其中加法意为 ...

  5. SQL千万级数据设计和优化

    1. 数据太多.放在一个表肯定不行. 比如月周期表.一个月1000万,一年就1.2亿,如此累计下去肯定不行的.所以都是基于一个周期数据一个表.甚至一个周期数据就要分几个分表.主要是考虑实际的数据量而定 ...

  6. H.264中NAL、Slice与frame意思及相互关系

    H.264中NAL.Slice与frame意思及相互关系 NAL nal_unit_type中的1(非IDR图像的编码条带).2(编码条带数据分割块A).3(编码条带数据分割块B).4(编码条带数据分 ...

  7. 2014-LAMP兄弟连视频下载地址汇总

    linux 兄弟连2014年新版Linux视频教程百度网盘下载 http://pan.baidu.com/s/1kTsjVfx http://pan.baidu.com/s/1sjJf2OX 兄弟连2 ...

  8. TCP/IP详解学习笔记(12)-TCP的超时与重传

    超时重传是TCP协议保证数据可靠性的另一个重要机制,其原理是在发送某一个数据以后就开启一个计时器,在一定时间内如果没有得到发送的数据报的ACK报文,那么就重新发送数据,直到发送成功为止. 1.超时 超 ...

  9. context.Response.End()的用法和本质

    using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace Web_C ...

  10. linux下利用backtrace追踪函数调用堆栈以及定位段错误

    一般察看函数运行时堆栈的方法是使用GDB(bt命令)之类的外部调试器,但是,有些时候为了分析程序的BUG,(主要针对长时间运行程序的分析),在程序出错时打印出函数的调用堆栈是非常有用的. 在glibc ...