2 回答
![?](http://img1.sycdn.imooc.com/533e51f30001edf702000200-100-100.jpg)
TA贡献1811条经验 获得超4个赞
我相信,如果您有单个接口的多个实现,那么您应该使用以下特定的 bean 名称。
这里 implementation1 将是我们在 Interface1 依赖项中创建和注入的主要 bean。
@Primary
@Bean
public Interface1 implementation1() {
return new Implementation2();
}
@Bean
public Interface1 implementation2() {
return new Implementation2();
}
如果我们需要注入 implementation2,我们需要@Resource 注释,如下所示。
@Resource(name="implementation2")
Interface1 implementation2;
![?](http://img1.sycdn.imooc.com/5458655200013d9802200220-100-100.jpg)
TA贡献1856条经验 获得超17个赞
您始终可以在使用特定 bean 的每个地方定义限定符:
@Bean
public SomeInterface beanName1(){ //impl }
@Bean
public SomeInterface beanName2(){ //impl }
用法:
@Qualifier("beanName1") SomeInterface interface;
还需要在您的application.yml/properties文件中允许多个 bean:
spring.main.allow-bean-definition-overriding=true
添加回答
举报