title: Spring @Autowired Interface conditional resolution at runtime via CLI
body: <p>I'm new to Spring Boot and annotations in general. I have an Interface (ApiClientInterface) that I'd like to use the <code>@Autowired</code> annotation on a property to resolve. For the sake of this post, assume I have two implementations...</p>
  
  <p>Implementation one:</p>
  
  <pre><code>package com.java.app.apiclient;
  
  @Component
  class HttpRestAdapter implements ApiClient {
      ...
  }
  </code></pre>
  
  <p>Implementation two:</p>
  
  <pre><code>package com.java.app.apiclient;
  
  @Component
  class FakeAdapter implements ApiClient {
      ...
  }
  </code></pre>
  
  <p>I did some googling and found the <code>@Qualifier</code> annotation, but I'm not sure this is the right path, because this is a CLI application. Ideally, something like this would be possible:</p>
  
  <pre><code>$ java -jar myjar --apiclient=fake
  $ #  - OR -
  $ java -jar myjar --apiclient=resthttp
  </code></pre>
  
  <p>Depending on the value of that <code>--apiclient</code> argument, ideally the IoC container would know which implementation to resolve. In other words, I'd like this test to pass:</p>
  
  <pre><code>public class ApiServiceFactoryTest {
      @Autowired
      private ApiClient client;
  
      @Test
      public void testClientResolution() {
          assertEquals(client.getClass(), isA(FakeClient.class));
      }
  }
  </code></pre>
  
  <p>I am coming from the PHP world where the IoC Container bindings are more commonly registered explicitly in ServiceProviders where something like a factory could be used to determine which implementation should be registered. I'd like to stick with annotating &amp; injecting the interface, rather than injecting a factory and then using that factory to get the correct service.</p>
  
  <p>In Spring's case, the annotations are "hard coded", so it's not possible to do that resolution programmatically (at least not that I can find). So, how can I allow the container to resolve the implementation at runtime? Is it possible to accomplish this?</p>