title: Refactor conditional to polymorphism in spring-boot code
body: <p>I have seen example given in Refactoring by Martin Fowler <a href="http://refactoring.com/catalog/replaceConditionalWithPolymorphism.html" rel="nofollow">Here</a></p>
  
  <p>Not sure how I can implement it in spring-boot way(IoC).</p>
  
  <p>I am working on spring web application.
  I have a REST controller which accepts <code>studentId</code> and <code>fileType</code> and export data of <em>Students</em> in given <code>fileType</code> format.
  Controller calls <code>ExportService</code> <code>exportFile()</code> method which looks like</p>
  
  <pre><code>@Service
  public class ExportServiceImpl implements ExportService {
      public void exportFile(Integer studentId, String fileType) {
          if (XML.equals(fileType)) { exportXML(studentId);}
          else if()... // and so on for CSV, JSON etc
      }
  }
  </code></pre>
  
  <p>To refactor conditional to polymorphism,</p>
  
  <p>First I created abstract class,</p>
  
  <pre><code>abstract class ExportFile {
      abstract public void doExport(Integer studentId);
  }
  </code></pre>
  
  <p>Then I create services for each fileType export. For example XML export following is a service,</p>
  
  <pre><code>@Service
  public class ExportXMLFileService extends ExportFile {
      public void doExport(Integer studentId) {
          // exportLogic will create xml
      }
  }
  </code></pre>
  
  <p>Now my ExportService should look like,</p>
  
  <pre><code>@Service
  public class ExportServiceImpl implements ExportService {
      @Autowired
      private ExportFile exportFile;
  
      public void exportFile(Integer studentId, String fileType) {
          exportFile.doExport(studentId);
      }
  }
  </code></pre>
  
  <p>Now here I am stuck :( </p>
  
  <p>Could not get, 
  How the <code>@Autowired</code> <code>ExportFile</code> will know which concrete service to refer based on <code>fileType</code>?</p>
  
  <p>Please do correct me if I am wrong. Your response will be greatly appreciated :)</p>