Spring Autowiring @Qualifier example
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的更多相关文章
- 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 ...
- Spring Autowiring by AutoDetect
In Spring, "Autowiring by AutoDetect", means chooses "autowire by constructor" i ...
- Spring Autowiring by Constructor
In Spring, "Autowiring by Constructor" is actually autowiring by Type in constructor argum ...
- Spring Autowiring by Name
In Spring, "Autowiring by Name" means, if the name of a bean is same as the name of other ...
- Spring Autowiring by Type
In Spring, "Autowiring by Type" means, if data type of a bean is compatible with the data ...
- Spring Auto-Wiring Beans
In Spring framework, you can wire beans automatically with auto-wiring feature. To enable it, just d ...
- Spring的qualifier标签
@Autowired是根据类型进行自动装配的.如果当Spring上下文中存在不止一个UserDao类型的bean时,就会抛出BeanCreationException异常;如果Spring上下文中不存 ...
- Spring注解@Qualifier
在使用Spring框架中@Autowired标签时默认情况下使用 @Autowired 注释进行自动注入时,Spring 容器中匹配的候选 Bean 数目必须有且仅有一个.当找不到一个匹配的 Bean ...
- 【spring】@Qualifier注解
近期在捯饬spring的注解,现将遇到的问题记录下来,以供遇到同样问题的童鞋解决~ 先说明下场景,代码如下: 有如下接口: public interface EmployeeService { pub ...
随机推荐
- C#6.0 VS2015
https://msdn.microsoft.com/en-us/library/hh156499(v=vs.140).aspx This page lists key feature names f ...
- velocity加减运算注意格式 ,加减号的左右都要有空格
velocity加减运算注意格式 ,加减号的左右都要有空格 #set( $left= $!biz.value - $vMUtils.getReturnMoney($!biz.billBuy) )
- 安装IIS之后运行aspx 显示“服务器应用程序不可用” 解决办法
引起这个的原因大概是现安装了.Net Framework,后装的IIS导致.Net没有在IIS里注册. 另外,还有可能是ASPNET账户没有IIS所指定服务器目录的权限.在资源管理器中找到“工具-文 ...
- hdu 4602 Partition(快速幂)
推公式+快速幂 公式有很多形式,可以写矩阵 1.前n-1项和的两倍+2的(n-2)次方,这个写不出啥 2.递推式:f(n)=2*f(n-1)+2的(n-3)次方 3.公式:2的(n-k-2)次方*(n ...
- table中嵌套table,如何用jquery来控制奇偶行颜色
总是要趁着自己还有记忆的时候,把该记录下来的都记录下来,着实是不敢恭维自己的记性. 相信很多时候,我们前端人员,经常会用到table里面的某个td中还嵌套着table,而这个时候还总要去弄奇偶行的颜色 ...
- 通过js检测到iframe,使父窗口重定向到index -----------???----------------------
通过js检测到iframe,使父窗口重定向到index -----------???---------------------- 如果本身已将在iframe中,那么重定向的页面应该直接添加到父级ifr ...
- AE与AO的区别
在ArcGis9.0之前,ArcObject还不是一个独立的产品,一直捆绑在Desktop产品中,只要你购买了desktop产品中的一个,你就可 以使用arcboject开发.从ArcGis9.0开始 ...
- 滑动菜单栏开源项目SlidingMenu的使用
一.SlidingMenu简介相信大家对SlidingMenu都不陌生了,它是一种比较新的设置界面或配置界面的效果,在主界面左滑或者右滑出现设置界面效果,能方便的进行各种操作.很多优秀的应用都采用了这 ...
- 《Python CookBook2》 第一章 文本 - 控制大小写 && 访问子字符串
控制大小写 任务: 将一个字符串由大写转成小写,或者泛起到而行之. 解决方案: >>> a = 'a'.upper() >>> a 'A' >>> ...
- 记录一下学习Android时遇到一些问题
实在是不擅长Android开发,但在努力的学习当中.这篇文章就记录一下学习过程中,自己犯下的一些错误,同时也让自己记住别再犯同样的错误了.各位看官勿见笑! 一个关于空指针的错误 错误类型一: 未对对象 ...