Here is my question:I have a base interface and two implementation class.
And a Service class has a dependencies on the base interface, the code is like this:
@Component
public interface BaseInterface {}
@Component
public class ClazzImplA implements BaseInterface{}
@Component
public class ClazzImplB implements BaseInterface{}
And the configuration is like this :
@Configuration
public class SpringConfig {
@Bean
public BaseInterface clazzImplA(){
return new ClazzImplA();
}
@Bean
public BaseInterface clazzImplB(){
return new ClazzImplB();
}
}
The service class has dependencies on the base interface will decide to autowire which Implementation by some business logic.And the code is like this:
@Service
@SpringApplicationConfiguration(SpringConfig.class)
public class AutowiredClazz {
@Autowired
private BaseInterface baseInterface;
private AutowiredClazz(BaseInterface baseInterface){
this.baseInterface = baseInterface;
}
}
And the IDEA throws a exception:Could not autowire.There is more than one bean of BaseInterface
type.
Although it can be solved by using @Qualifier,but in this situation I can't choose the dependencies class.
@Autowired
@Qualifier("clazzImplA")
private BaseInterface baseInterface;
I tried to read the spring document and it provide a Constructor-based dependency injection
but I'm still confused by the problem.
can anyone help me ?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…