title: Spring autowire multiple service Implementations
body: <p>I have one base interface and two implementations</p>
  
  <pre><code>public interface AnimalService
  {
      public void eat();
  }
  
  @Service("animalService")
  @Transactional
  public class AnimalServiceImpl implements AnimalService
  {
      @Override
      public void eat()
      {
          System.out.println("i'm eating");
      }
  }
  
  @Service("birdService")
  @Transactional
  public class BirdServiceImpl extends AnimalServiceImpl
  {
      public void fly()
      {
          System.out.println("i'm flying");
      }
  }
  </code></pre>
  
  <p>In my main method try to call this two service implementation in this way:</p>
  
  <pre><code>public class test
  {
      @Autowired
      private AnimalService animalService;
      @Autowired
      @Qualifier("birdService")
      private AnimalService birdService;
  
      public static void main(String[] args)
      {
          animalService.eat();
          birdService.eat();
          birdService.fly();
      }
  }
  </code></pre>
  
  <p>This will give compilation error, since birdService can't find method fly(). Then I thought maybe the reason is i autowire AnimalService instead of BirdServiceImpl, So i change my autowire code from this:</p>
  
  <pre><code>   @Autowired
     @Qualifier("birdService")
     private AnimalService birdService;
  </code></pre>
  
  <p>change to :</p>
  
  <pre><code>   @Autowired
     private BirdServiceImpl birdService;
  </code></pre>
  
  <p>But this will give me a runtime error, which is "can't find bean BirdServiceImpl".
  I have google a lot of document, some say use @Resource. But this doesn't work for me. Some say register the bean in Spring Context, while all my bean registration is done by annotation. I don't want to touch Spring Context.</p>
  
  <p>Now My solution is to add a new interface </p>
  
  <pre><code>public interface BirdService extends AnimalService
  {
      public void fly();
  }
  </code></pre>
  
  <p>And let my BirdServiceImpl to implement this interface</p>
  
  <pre><code>    public class BirdServiceImpl extends AnimalServiceImpl extends BirdService
      {
          public void fly()
          {
              System.out.println("i'm flying");
          }
      }
  </code></pre>
  
  <p>And my main class change to this: </p>
  
  <pre><code>public class test
  {
      @Autowired
      private AnimalService animalService;
      @Autowired
      private BirdService birdService;
  
      public static void main(String[] args)
      {
          animalService.eat();
          birdService.eat();
          birdService.fly();
      }
  }
  </code></pre>
  
  <p>Now is ok . But for me, this is not perfect. If I use plain java, i can just write single interface and multiple implementation. In the main method I can choose which implementation to use. Why in spring, i have to build a new interface for each new implementation in order to let my program run.</p>
  
  <p>I want to know is there any better approach for my scenario?</p>