当找到多个匹配的bean时,Spring如何按名称自动装配? [英] How does Spring autowire by name when more than one matching bean is found?

查看:266
本文介绍了当找到多个匹配的bean时,Spring如何按名称自动装配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有这样的接口:

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

这是配置xml的代码段:

And this snippet of configuration xml:

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

如何控制下面自动关联的依赖项?我想要英国.

How can I control which dependency is autowired below? I'd like the UK one.

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

我正在使用Spring 3.0.3.RELEASE.

I am using Spring 3.0.3.RELEASE.

推荐答案

对于后备匹配,bean名称被认为是默认的限定符值.

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

换句话说,默认行为就像您将@Qualifier("country")添加到setter方法中一样.

In other words, the default behaviour is as though you'd added @Qualifier("country") to the setter method.

这篇关于当找到多个匹配的bean时,Spring如何按名称自动装配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆