相关疑难解决方法(0)

当找到多个匹配的bean时,Spring如何按名称自动装配?

假设我有这样的接口:

interface Country {}
class USA implements Country {}
class UK implements Country ()
Run Code Online (Sandbox Code Playgroud)

这个配置xml片段:

<bean class="USA"/>
<bean id="country" class="UK"/>
<bean id="main" class="Main"/>
Run Code Online (Sandbox Code Playgroud)

如何控制下面自动连接的依赖项?我想要英国人.

class Main {
    private Country country;
    @Autowired
    public void setCountry(Country country) {
        this.country = country;
    }
}
Run Code Online (Sandbox Code Playgroud)

我使用的是Spring 3.0.3.RELEASE.

spring

102
推荐指数
5
解决办法
14万
查看次数

2个具有相同名称但位于不同包中的bean; 如何自动装配它们?

我有一个应用程序有2个同名的bean,但它们在不同的包中.我的Spring应用程序失败,因为它无法决定采用哪个bean.这有什么解决方案吗?Bean目前不实现特定的接口.

请参阅下面一个已编辑的异常示例:

Caused by:
org.springframework.context.annotation.ConflictingBeanDefinitionException:
Annotation-specified bean name 'dataTransferHandler' for bean class
[aaaaa.ws.handler.DataTransferHandler] conflicts with existing,
non-compatible bean definition of same name and class
[bbbbb.ws.handler.DataTransferHandler]
Run Code Online (Sandbox Code Playgroud)

spring inversion-of-control autowired

11
推荐指数
1
解决办法
2万
查看次数

标签 统计

spring ×2

autowired ×1

inversion-of-control ×1