title: How to inject different services at runtime based on a property with Spring without XML
body: <p>I am using Spring Boot for Java standalone application. I have a bean which makes use of a service. I want to inject different implementations of that service at runtime, based on a property in a properties file with Spring (4 for that matter).</p>
  
  <hr>
  
  <p>This sounds like the Factory pattern, but Spring also allows using annotations to solve the problem, like this.</p>
  
  <pre><code>@Autowired @Qualifier("selectorProperty") private MyService myService;
  </code></pre>
  
  <p>Then in the beans.xml file I have an alias, so that I can use the property in the @Qualifier.</p>
  
  <pre><code>&lt;alias name="${selector.property}" alias="selectorProperty" /&gt;
  </code></pre>
  
  <p>And in my different implementations I would have different qualifiers.</p>
  
  <pre><code>@Component("Selector1")
  public class MyServiceImpl1
  
  @Component("Selector2")
  public class MyServiceImpl2
  </code></pre>
  
  <p>application.properties</p>
  
  <pre><code>selector.property = Selector1
  
  selector.property = Selector2
  </code></pre>
  
  <hr>
  
  <p>Whereas regarding the factory pattern, in Spring you can use ServiceLocatorFactoryBean to create a factory that would give you the same functionality.</p>
  
  <pre><code>&lt;bean
    class="org.springframework.beans.factory.config.ServiceLocatorFactoryBean"
    id="myServiceFactory"&gt;
    &lt;property
      name="serviceLocatorInterface"
      value="my.company.MyServiceFactory"&gt;
    &lt;/property&gt;
  &lt;/bean&gt;
  
  public interface MyServiceFactory
  {
      MyService getMyService(String selector);
  }
  </code></pre>
  
  <p>And then in your bean you can use something like this to get the right implementation at runtime depending on the value of the property.</p>
  
  <pre><code>@Value("${selector.property}") private String selectorProperty;
  
  @Autowired private MyServiceFactory myServiceFactory;
  
  private MyService myService;
  
  @PostConstruct
  public void postConstruct()
  {
      this.myService = myServiceFactory.getMyService(selectorProperty);
  }
  </code></pre>
  
  <p>But the problem with this solution is that I could not find a way to avoid using XML to define the factory, and I would like to use only annotations.</p>
  
  <hr>
  
  <p>So the question would be, is there a way to use the ServiceLocatorFactoryBean (or something equivalent) using only annotations, or am I forced to use the @Autowired @Qualifier way if I do not want to define beans in XML? Or is there any other way to inject different services at runtime based on a property with Spring 4 avoiding XML? If your answer is just use the <code>@Autowired @Qualifier</code> with the alias, please give a reason why that is better than using a well known factory pattern.</p>
  
  <p>Using the extra XML is forcing me to use <code>@ImportResource("classpath:beans.xml")</code> in my Launcher class, which I'd rather not use either.</p>
  
  <p>Thanks.</p>