弹簧无法自动布线,有多个“”类型的豆子
这是我的问题:我有一个基本接口和两个实现类。
服务类在基接口上有一个依赖项,代码是这样的:
@Component
public interface BaseInterface {}
@Component
public class ClazzImplA implements BaseInterface{}
@Component
public class ClazzImplB implements BaseInterface{}
配置是这样的:
@Configuration
public class SpringConfig {
@Bean
public BaseInterface clazzImplA(){
return new ClazzImplA();
}
@Bean
public BaseInterface clazzImplB(){
return new ClazzImplB();
}
}
服务类具有依赖关系,基接口将决定通过某些业务逻辑自动连接哪个实现。代码是这样的:
@Service
@SpringApplicationConfiguration(SpringConfig.class)
public class AutowiredClazz {
@Autowired
private BaseInterface baseInterface;
private AutowiredClazz(BaseInterface baseInterface){
this.baseInterface = baseInterface;
}
}
IDEA抛出一个异常:无法自动连线。有不止一种豆类类型。BaseInterface
虽然可以通过使用@Qualifier来解决,但是在这种情况下我无法选择依赖类。
@Autowired
@Qualifier("clazzImplA")
private BaseInterface baseInterface;
我试图阅读Spring文档,它提供了一个基于构造函数的依赖注入
,但我仍然对这个问题感到困惑。
任何人都可以帮助我吗?