gpt4 book ai didi

spring - 当找到多个匹配的bean时,Spring如何按名称 Autowiring ?

转载 作者:IT老高 更新时间:2023-10-28 13:01:11 26 4
gpt4 key购买 nike

假设我有这样的接口(interface):

interface Country {}
class USA implements Country {}
class UK implements Country ()

还有这段配置xml:

<bean class="USA"/>
<bean id="country" class="UK"/>
<bean id="main" class="Main"/>

如何控制下面 Autowiring 的依赖项?我想要英国的。

class Main {
private Country country;
@Autowired
public void setCountry(Country country) {
this.country = country;
}
}

我正在使用 Spring 3.0.3.RELEASE。

最佳答案

这在 section 3.9.3 中有记录Spring 3.0 手册:

For a fallback match, the bean name is considered a default qualifier value.

换句话说,默认行为就像您在 setter 方法中添加了 @Qualifier("country")

关于spring - 当找到多个匹配的bean时,Spring如何按名称 Autowiring ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4447877/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com