title: Injecting multiple instances of different beans in Spring
body: <p>I am trying to solve a problem with the Spring DI. I have two beans (<code>MyFirstBean</code> &amp; <code>MySecondBean</code>) that both implement a given interface (<code>MyBean</code>). Then I have multiple other beans (e.g. <code>OtherBean</code>) that I want to use with either one of the two beans. Autowiring obviously fails for <code>OtherBean</code> since there are multiple instances of <code>MyBean</code> to choose from. Is there any possibility to generically create two instances of each bean 
   that autowires <code>MyBean</code> and refer to them using qualifiers? I know this is possible by writing a configuration class but since all this is part of an API, I want to keep the overhead as low as possible.</p>
  
  <p><strong>Current Situation:</strong></p>
  
  <pre><code>public interface MyBean {
  }
  
  @Component
  public class MyFirstBean implements MyBean {
  }
  
  @Component
  public class MySecondBean implements MyBean {
  }
  
  @Component
  public class OtherBean {
  
      final MyBean myBean; // error due to multiple beans
  
      public OtherBean(MyBean myBean) {
          this.myBean = myBean;
      }
  }
  </code></pre>
  
  <p><strong>Desired Situation:</strong></p>
  
  <pre><code>@Component
  public class SomeBean {
  
      final OtherBean myBeanUsingFirstBean; // internally autowires MyFirstBean
  
      final OtherBean myBeanUsingSecondBean; // internally autowires MySecondBean
  
      public SomeBean(
          @FirstBeanQualifier OtherBean myBeanUsingFirstBean,
          @SecondBeanQualifier OtherBean myBeanUsingSecondBean) {
          this.myBeanUsingFirstBean = myBeanUsingFirstBean;
          this.myBeanUsingSecondBean = myBeanUsingSecondBean;
      }
  }
  </code></pre>