zoukankan      html  css  js  c++  java
  • @Qualifier注解详解

    @Qualifier注解意味着可以在被标注bean的字段上可以自动装配。Qualifier注解可以用来取消Spring不能取消的bean应用。

    下面的示例将会在Customer的person属性中自动装配person的值。

    1
    2
    3
    4
    5
    public class Customer
    {
        @Autowired
        private Person person;
    }

    下面我们要在配置文件中来配置Person类。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    <bean id="customer" class="com.howtodoinjava.common.Customer" />
     
    <bean id="personA" class="com.howtodoinjava.common.Person" >
        <property name="name" value="lokesh" />
    </bean>
     
    <bean id="personB" class="com.howtodoinjava.common.Person" >
        <property name="name" value="alex" />
    </bean>

    Spring会知道要自动装配哪个person bean么?不会的,但是运行上面的示例时,会抛出下面的异常:

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

    要解决上面的问题,需要使用 @Quanlifier注解来告诉Spring容器要装配哪个bean:

    1
    2
    3
    4
    5
    6
    public class Customer
    {
        @Autowired
        @Qualifier("personA")
        private Person person;
    }
  • 相关阅读:
    事事浑不着意,总得有走心的地方
    Struts2框架概述
    java算法数据结构
    shell十三问(转)
    linux:将job放在后台执行的方法
    Python性能(转)
    python collections deque
    python中的参数传递和返回值
    python中的下划线
    python学习笔记glob模块
  • 原文地址:https://www.cnblogs.com/wirr/p/8487113.html
Copyright © 2011-2022 走看看