title: How to inject a bean based on the property of another bean?
body: <p>I am trying to apply <code>Strategy</code> pattern (which I think I get it wrong) using Spring as following  </p>
  
  <p>My main class looks like</p>
  
  <pre><code>@Component
  public class DirectoryUserImportWorkflow {
      private List&lt;DirectoryUserDataSource&gt; dataSources = Arrays.asList(new ActiveDirectoryDataSource(), new CsvDataSource());
  
      @Autowired
      private DirectoryUsersFetcher directoryUsersFetcher;
  
      public void run() {
          dataSources.forEach(dataSource -&gt; directoryUsersFetcher.importUsers(dataSource));
      }
  }
  </code></pre>
  
  <p>where <code>DirectoryUsersFetcher</code> is an interface as  </p>
  
  <pre><code>public interface DirectoryUsersFetcher {
      Iterator&lt;String&gt; importUsers(DirectoryUserDataSource dataSource);
  }
  </code></pre>
  
  <p>with 2 implementations as  </p>
  
  <pre><code>@Component
  public class ActiveDirectoryUsersFetcher implements DirectoryUsersFetcher {
      public Iterator&lt;String&gt; importUsers(DirectoryUserDataSource dataSource) {
          System.out.println("Returning data from Active Directory");
          return Arrays.asList("ActiveDirectoryUser1", "ActiveDirectoryUser2", "ActiveDirectoryUser3").iterator();
      }
  }
  </code></pre>
  
  <p>and  </p>
  
  <pre><code>@Component
  public class CsvUsersFetcher implements DirectoryUsersFetcher {
      public Iterator&lt;String&gt; importUsers(DirectoryUserDataSource dataSource) {
          System.out.println("Returning data from CSV");
          return Arrays.asList("CsvUser1", "CsvUser2", "CsvUser3").iterator();
      }
  }
  </code></pre>
  
  <p>I want one of them to be used at Runtime based on what the <code>DataSourceType</code> is  </p>
  
  <pre><code>public enum DataSourceType {
      DirectoryServer,
      Csv
  }
  </code></pre>
  
  <p>The <code>DataSource</code> itself is an interface which looks like  </p>
  
  <pre><code>public interface DirectoryUserDataSource {
      DataSourceType getType();
  }
  </code></pre>
  
  <p>with 2 implementations as  </p>
  
  <pre><code>public class ActiveDirectoryDataSource implements DirectoryUserDataSource {
      public DataSourceType getType() {
          return DataSourceType.DirectoryServer;
      }
  }
  </code></pre>
  
  <p>and  </p>
  
  <pre><code>public class CsvDataSource implements DirectoryUserDataSource {
      public DataSourceType getType() {
          return DataSourceType.Csv;
      }
  }
  </code></pre>
  
  <p>My <code>test</code> looks like  </p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration(classes = {DirectoryUserImportWorkflow.class, ActiveDirectoryUsersFetcher.class, CsvUsersFetcher.class})
  public class DirectoryUserImportWorkflowTest {
  
      @Autowired
      private DirectoryUserImportWorkflow workflow;
  
      @Test
      public void runStrategy() throws Exception {
          workflow.run();
      }
  }
  </code></pre>
  
  <p>What I see is  </p>
  
  <pre><code>        at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)
  Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'directoryUserImportWorkflow': Unsatisfied dependency expressed through field 'directoryUsersFetcher': No qualifying bean of type [com.learner.datafetcher.DirectoryUsersFetcher] is defined: expected single matching bean but found 2: activeDirectoryUsersFetcher,csvUsersFetcher; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.learner.datafetcher.DirectoryUsersFetcher] is defined: expected single matching bean but found 2: activeDirectoryUsersFetcher,csvUsersFetcher
  </code></pre>
  
  <p>How can I solve this problem?</p>
  
  <p><strong>What I need?</strong> </p>
  
  <p>Based on what <code>DataSource</code> is <code>ActiveDirectory</code> or <code>Csv</code>, the specific fetcher should invoke <code>ActiveDirectoryUsersFetcher</code> or <code>CsvUsersFetcher</code></p>
  
  <p>Where am I missing the understanding?</p>
  
  <p>Thanks in advance</p>