spring - common annotation @Qualifier

1. Introduction

@Qualifier is a very practical annotation under org.springframework.beans.factory.annotation. Its description is:

* This annotation can be used as a qualifier for fields or parameters

* Candidate beans for autowiring. It can also be used to annotate other

* Custom annotations, which can then be used as qualifiers

2. Usage scenarios

When there are two beans of the same type, we don't know which bean we need to introduce during automatic injection, so we need @Qualifier ("xxxx") to specify at this time.
otherwise it will appear

org.springframework.beans.factory.NoUniqueBeanDefinitionException:
 No qualifying bean of type 'com.xxx.xxx' available: 
 expected single matching bean but found xxx次数

example:

  
    @Bean("queueD")
    public Queue queueD() {
    
    
        return new Queue(DEAD_LETTER_QUEUE);
    }
    @Bean("queueB")
    public Queue queueB() {
    
    
        return new Queue(DEAD_LETTER_QUEUE);
    }
    
   @Bean
   //在此执行我们要导入某个类型下的哪一个进来
    public void deadLetterBindingQAD(@Qualifier("queueD") Queue){
    
    
    	.....
    } 

Guess you like

Origin blog.csdn.net/weixin_46266624/article/details/131291265