Please go through complete question before marking duplicate. Can discuss more in comments
I have following code where ServiceA depends on ServiceB. And serviceB implementation bean will be conditionally initialized
class ServiceA{
ServiceB serviceB;
ServiceA(ServiceB serviceB){
this.serviceB = serviceB;
}
}
@Configuration
class AppConfig{
@Conditional("some_condition_based_on_property")
@Bean
ServiceB serviceB1(){
return new ServiceBImpl1();
}
@Conditional("some_condition_based_on_property")
@Bean
ServiceB serviceB2(){
return new ServiceBImpl2();
}
@Bean
ServiceA serviceA(){
//what should go here so that conditional bean is injected in ServiceA
}
}
I cannot auto-detect ServiceA bean, as I need to inject it in a Map with some key. One option I see is to remove construction injection, and have serviceB bean @autowired in serviceA, which would be last option for me. Any other option?
Edit: I don't want to have if-else during injection, as the beans can be defined at various places. I will be only using @Conditional