title: How to dynamically inject a service using a runtime "qualifier" variable?
body: <p>I can't find a simple way to inject a component/service given a runtime value.</p>
  
  <p>I started reading @ Spring's doc: <a href="http://docs.spring.io/spring/docs/current/spring-framework-reference/html/beans.html#beans-autowired-annotation-qualifiers" rel="noreferrer">http://docs.spring.io/spring/docs/current/spring-framework-reference/html/beans.html#beans-autowired-annotation-qualifiers</a>
  but I can't find there how to variabilize the values passed to the @Qualifier annotation.</p>
  
  <p>Let's say I've got a model entity with such interface:</p>
  
  <pre><code>public interface Case {
  
      String getCountryCode();
      void setCountryCode(String countryCode);
  
  }
  </code></pre>
  
  <p>In my client code, I would do something like:</p>
  
  <pre><code>@Inject
  DoService does;
  
  (...)
  
  Case myCase = new CaseImpl(); // ...or whatever
  myCase.setCountryCode("uk");
  
  does.whateverWith(myCase);
  </code></pre>
  
  <p>... with my service being:</p>
  
  <pre><code>@Service
  public class DoService {
  
      @Inject
      // FIXME what kind of #$@&amp; symbol can I use here?
      // Seems like SpEL is sadly invalid here :(
      @Qualifier("${caze.countryCode}")
      private CaseService caseService;
  
      public void whateverWith(Case caze) {
          caseService.modify(caze);
      }
  
  }
  </code></pre>
  
  <p>I expect the caseService to be the UKCaseService (see related code below).</p>
  
  <pre><code>public interface CaseService {
  
      void modify(Case caze);
  
  }
  
  @Service
  @Qualifier("uk")
  public class UKCaseService implements CaseService {
  
  }
  
  @Service
  @Qualifier("us")
  public class USCaseService implements CaseService {
  
  }
  </code></pre>
  
  <p>So how do I "fix" all of this in the most simple / elegant / efficient way by using either/all Spring feature(s), so essentially NO .properties, NO XML, only annotations.
  However I already suspect something is wrong in my DoService because Spring would need to know the "case" before injecting the caseService... but how to achieve this without the client code knowing about the caseService?!
  I can't figure this out...</p>
  
  <p>I already read several issues here on SO, but most of the times either they don't really have the same needs and/or config as I have, or the posted answers aren't enough satisfying to me (look like they're essentially workarounds or (old) usage of (old) Spring features).</p>
  
  <p><a href="https://stackoverflow.com/questions/4447877/how-does-spring-autowire-by-name-when-more-than-one-matching-bean-is-found">How does Spring autowire by name when more than one matching bean is found?</a>
  => only refers to component-like classes</p>
  
  <p><a href="https://stackoverflow.com/questions/7720454/dynamically-defining-which-bean-to-autowire-in-spring-using-qualifiers">Dynamically defining which bean to autowire in Spring (using qualifiers)</a>
  => really interesting but the most elaborated answer (4 votes) is... almost 3 1/2 years-old?! (July 2013)</p>
  
  <p><a href="https://stackoverflow.com/questions/15284851/spring-3-dynamic-autowiring-at-runtime-based-on-another-object-attribute">Spring 3 - Dynamic Autowiring at runtime based on another object attribute</a>
  => quite similar problem here, but the answer really look like a workaround rather a real design pattern (like factory)? and I don't like implementing all the code into the ServiceImpl as it's done...</p>
  
  <p><a href="https://stackoverflow.com/questions/277494/spring-autowiring-how-to-use-an-object-factory-to-choose-implementation">Spring @Autowiring, how to use an object factory to choose implementation?</a>
  => 2nd answer seems interestingly but its author does not expand, so altough I know (a bit) about Java Config &amp; stuff, I'm not really sure what he's talking about...</p>
  
  <p><a href="https://stackoverflow.com/questions/26463393/how-to-inject-different-services-at-runtime-based-on-a-property-with-spring-with">How to inject different services at runtime based on a property with Spring without XML</a>
  => interesting discussion, esp. the answer, but the user has properties set, which I don't have.</p>
  
  <p>Also read this:
  <a href="http://docs.spring.io/spring/docs/current/spring-framework-reference/html/expressions.html#expressions-bean-references" rel="noreferrer">http://docs.spring.io/spring/docs/current/spring-framework-reference/html/expressions.html#expressions-bean-references</a>
  => I can't find expanded examples about the use of "@" in expressions. Does someone know about this?</p>
  
  <p>Edit:
  Found other related-to-similar issues, no one got a proper answer:
  <a href="https://stackoverflow.com/questions/5725222/how-to-use-autowired-to-dynamically-inject-implementation-like-a-factory-patter">How to use @Autowired to dynamically inject implementation like a factory pattern</a>
  <a href="https://stackoverflow.com/questions/7812745/spring-qualifier-and-property-placeholder">Spring Qualifier and property placeholder</a>
  <a href="https://stackoverflow.com/questions/18209237/spring-using-qualifier-with-property-placeholder">Spring: Using @Qualifier with Property Placeholder</a>
  <a href="https://stackoverflow.com/questions/19225115/how-to-do-conditional-auto-wiring-in-spring">How to do conditional auto-wiring in Spring?</a>
  <a href="https://stackoverflow.com/questions/26052910/dynamic-injection-in-spring">Dynamic injection in Spring</a>
  <a href="https://stackoverflow.com/questions/29772418/spel-in-qualifier-refer-to-same-bean">SpEL in @Qualifier refer to same bean</a>
  <a href="https://stackoverflow.com/questions/38740809/how-to-use-spel-to-inject-result-o4f-method-call-in-spring">How to use SpEL to inject result of method call in Spring?</a></p>
  
  <p>Factory Pattern might be a solution?
  <a href="https://stackoverflow.com/questions/5725222/how-to-use-autowired-to-dynamically-inject-implementation-like-a-factory-patter">How to use @Autowired to dynamically inject implementation like a factory pattern</a></p>