title: Dependency Injection with interface in Spring
body: <p>I've a <strong>MainHandler</strong> class :</p>
  
  <pre><code>@Component
  class MainHandler {
  
      //inject this
      private Handler handler;
  
      @Autowired
      public MainHandler(Handler handler){
         this.handler = handler;
      }
  
      public void action(String message){
          //watch photo
          if (message.equals("photo")){
              handler.handle();
          }
          if(message.equals("audio")){
              //play music
              handler.handle();
          }
          if(message.equals("video")){
              //play video
              handler.handle();
          }
      }
  </code></pre>
  
  <p>And following other handlers with interface. </p>
  
  <p>Can I inject dependencies with Spring Boot by only interface type handler?</p>
  
  <pre><code>@Component
  public interface Handler {
      void handle();
  }
  
  @Component
  class PhotoHandler implements Handler {
      public void handle(){
          System.out.println("Featuring photo...");
      }
  }
  
  @Component
  class VideoHandler implements Handler {
      public void handle(){
          System.out.println("Playing video...");
      }
  }
  
  @Component
  class AudioHandler implements Handler {
      public void handle(){
          System.out.println("Playing music...");
      }
  }
  
  </code></pre>
  
  <p>Or I want to try something like this below. Is it possible ?</p>
  
  <pre><code>class MainHandler {
  
      private VideoHandler videoHandler;
      private AudioHandler audioHandler;
      private PhotoHandler photoHandler;
  
      @Autowired
      public MainHandler(VideoHandler videoHandler,
                         AudioHandler audioHandler,
                         PhotoHandler photoHandler) {
          this.videoHandler = videoHandler;
          this.audioHandler = audioHandler;
          this.photoHandler = photoHandler;
      }
  
      public void action(String message){
          //watch photo
          if (message.equals("photo")){
              photoHandler.handle();
          }
          if(message.equals("audio")){
              //play music
              audioHandler.handle();
          }
          if(message.equals("video")){
              //play video
              videoHandler.handle();
          }
      }
  }
  </code></pre>
  
  <p>So, type of handler depends on user's message. I don't know how Spring can choose which handler gonna be used in this context. Any solution?</p>