简体   繁体   English

在Spring中在XML中使用Qualifier注释的替代方法是什么?

[英]What is the alternative of using Qualifier annotation in XML in Spring?

In Spring , if there are two bean ids which refer to the same class and we just want to inject values from only one bean, then we normally use the following annotations in conjunction : 在Spring中,如果有两个引用相同类的bean id,而我们只想从一个bean中注入值,那么通常我们将以下注释结合使用:

@Autowired
@Qualifier("bean1")

How to achieve the same thing using the xml specification ? 如何使用xml规范实现同一目的? What is the alternative of using qualifier annotation in xml ? 在xml中使用限定符注释的替代方法是什么?

Not an exact alternative but you can use autowire-candidate="false" to all those beans which you want to exclude from being autowired apart from the one which is to be autowired. 不是一种确切的选择,但是您可以对要排除在自动装配之外的所有那些bean使用autowire-candidate="false" ,除了要自动装配的那些。

Also you need to specify that particular bean which is eligible for autowiring by explicitly marking primary="true" for it and primary="false" for rest of them. 另外,您还需要通过显式地标记primary="true"并将其余的标记为primary="false"来指定适合自动装配的特定bean。

So roughly your xml configuration should look like below when you expect bean1 to be autowired 因此,当您希望自动装配bean1时,您的xml配置大致应如下所示
<bean id="bean1" class="xyzClassA" primary="true" autowire-candidate="true"/> <bean id="bean2" class="xyzClassA" primary="false" autowire-candidate="false"/> <bean id="bean3" class="xyzClassA" primary="false" autowire-candidate="false"/>

Do note that both autowire-candidate and primary are properties for beans tag and has default value as true . 请注意, autowire-candidateprimary都是beans标记的属性,默认值为true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM