I have an interface called MyService
and several implementations that I've added @Qualifier
to, such as A, B, etc. I'm trying to set up environment profiles such that I can select which one with a simple property file. Right now I have to do something like this:
@Value("${my.service.impl}")
private String myServiceImpl;
@Bean
public MyService myService() {
if (myServiceImpl.equals("A")) {
return new MyServiceA();
}
else if (myServiceImpl.equals("B")) {
return new MyServiceB();
}
// ...
}
But ideally I'd be able to just do something like this:
@Autowired
@Qualifier("${my.service.impl}")
public MyService myService;
However as far as I can find @Qualifier doesn't parse expressions. Does it not support expressions? More importantly, is there a cleaner way than an if/else to tell Spring which one to load?