spring - 共通アノテーション @Qualifier

1. はじめに

@Qualifier は org.springframework.beans.factory.annotation の下にある非常に実用的なアノテーションです。

* このアノテーションはフィールドまたはパラメータの修飾子として使用できます

* 自動配線の候補 Bean。他のものに注釈を付けるためにも使用できます

* カスタム注釈。修飾子として使用できます。

2. 利用シーン

同じ種類の Bean が 2 つある場合、自動インジェクションの際にどちらの Bean を導入すればよいかわからないため、このとき @Qualifier ("xxxx") を指定する必要があります。
そうしないと表示されます

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

例:

  
    @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){
    
    
    	.....
    } 

おすすめ

転載: blog.csdn.net/weixin_46266624/article/details/131291265