title: In Spring JavaConfig, should the bean return type be the interface or implementation?
body: <p>When I configure a class that implements <code>DataSource</code> (for example <a href="https://github.com/brettwooldridge/HikariCP" rel="nofollow">HikariCP</a>) via XML, it looks something like this:</p>
  
  <pre><code>&lt;bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close"/&gt;
  </code></pre>
  
  <p>According to the <a href="http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#beans-java-bean-annotation" rel="nofollow">Spring Reference manual</a>, the equivalent of this in JavaConfig is:</p>
  
  <pre><code>@Bean (destroyMethod = "close")
  public DataSource dataSource () {
      return new HikariDataSource();
  }
  </code></pre>
  
  <p>Why are we returning the interface type in JavaConfig, especially in this case, where <code>DataSource</code> doesn't have a <code>close()</code> method, but the implementation does (and somehow Spring does manage to find the <code>close()</code> method)?</p>
  
  <p>I couldn't find any difference in the application's behavior between this configuration method to using the implementation's type:</p>
  
  <pre><code>@Bean (destroyMethod = "close")
  public HikariDataSource dataSource () {
      return new HikariDataSource();
  }
  </code></pre>
  
  <p>Even when considering autowiring this shouldn't be a problem, since both return types will work for a <code>DataSource</code> instance variable.
  So what is the correct type to return (if there is a 'correct' one), and why?</p>