0

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?

css
  • 944
  • 1
  • 9
  • 27
  • Just in case: http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – Sotirios Delimanolis Jun 03 '14 at 17:58
  • As far as I've determined if anyone looks at this in the future, in Spring 4 there's no support for expressions in qualifiers, and there's also not a cleaner way to do this. – css Jun 06 '14 at 15:20

0 Answers0