Spring: Attaching @Qualifer to Java-configured beans

前端 未结 2 1901
暗喜
暗喜 2020-12-16 14:05

In spring, you can XML-configure a bean to have a qualifier. I can\'t seem to find how I can attach a qualifier if configuring beans through Java annotations. What\'s up wit

2条回答
  •  渐次进展
    2020-12-16 14:50

    If you're using annotations (not Java based configuration), you can use the following to add a qualifier (see the Spring documentation):

    @Component
    @Qualifier("myQualifier")
    public class MyBean {
        //code
    }
    

    And to wire in the bean, use the following (again, see the Spring documentation):

    public class MyClass {
    
        @Autowired
        @Qualifier("myQualifier")
        private MyBean myBean;
    
        //more code
    
    }
    

提交回复
热议问题