title: Spring 4 not automatically qualifying generic types on autowire
body: <p><strong>PROBLEM HAS BEEN IDENTIFIED, POST UPDATED</strong> (Scroll to bottom)</p>
  
  <p>I am developing a desktop application currently using Spring (<code>spring-context</code>, <code>4.1.6.RELEASE</code>) for IoC and dependency injection. I am using an annotation configuration, using <code>@ComponentScan</code>. The issue I am experiencing is supposed to be implemented as a feature in <code>4.X.X</code>, as it states <a href="https://jira.spring.io/browse/SPR-9965" rel="nofollow">here</a> and <a href="http://www.jayway.com/2013/11/03/spring-and-autowiring-of-generic-types/" rel="nofollow">here</a>, but I am getting the old <code>3.X.X</code> exception.</p>
  
  <p>I have a parameterised interface that represents a generic repository:</p>
  
  <pre><code>public interface DomainRepository&lt;T&gt; {
  
      T add(T entity) throws ServiceException, IllegalArgumentException;
  
      // ...etc
  
  }
  </code></pre>
  
  <p>I then have two concrete implementations of this, <code>ChunkRepositoryImpl</code> and <code>ProjectRepositoryImpl</code>, which are parameterised accordingly. They share some common implementation from an abstract class, but are declared like so:</p>
  
  <pre><code>@Repository
  public class ChunkRepositoryImpl extends AbstractRepositoryImpl&lt;Chunk&gt; implements DomainRepository&lt;Chunk&gt; {
  
      // ...+ various method implementations
  
  }
  
  @Repository
  public class ProjectRepositoryImpl extends AbstractRepositoryImpl&lt;Project&gt; implements DomainRepository&lt;Project&gt; {
  
      // ...+ various method implementations
  
  }
  </code></pre>
  
  <p>My understanding of the above links leads me to believe that I should be able to autowire these without needing to manually specify the beans via <code>@Qualifier</code>. However, when I do so:</p>
  
  <pre><code>@Autowired
  private DomainRepository&lt;Project&gt; repository;
  </code></pre>
  
  <p>I get the following exception (preceded by a long stack trace of course):</p>
  
  <blockquote>
    <p>Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.foo.bar.repositories.DomainRepository] is defined: expected single matching bean but found 2: chunkRepositoryImpl,projectRepositoryImpl</p>
  </blockquote>
  
  <p>Can anybody shine a light as to why this might be happening? I would expect this exception in <code>3.X.X</code>, but it should not happen in <code>4.X.X</code>. What is the difference between my situation, and the one described <a href="http://www.jayway.com/2013/11/03/spring-and-autowiring-of-generic-types/" rel="nofollow">here</a>?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I have discovered the source of the problem. One of the methods in my <code>DomainRepository&lt;T&gt;</code> interface is marked as <code>@Async</code>, and makes use of Spring's asynchronous capabilities. Removing this means that the beans are correctly qualified. I hypothesize that Spring transforms classes with <code>@Async</code> methods under the hood into some other class, and this process strips the type information, meaning that it can't tell the beans apart.</p>
  
  <p>This means I now have two questions:</p>
  
  <ol>
  <li>Is this intended behaviour?</li>
  <li>Can anybody suggest a workaround?</li>
  </ol>
  
  <p><a href="https://github.com/wfaithfull/spring-async-type-erasure" rel="nofollow">Here</a> is a project demonstrating the problem. Simply remove the <code>@Async</code> annotation from the <code>DomainRepository&lt;T&gt;</code> interface, and the problem dissappears.</p>