title: Autowiring two beans implementing same interface - how to set default bean to autowire?
body: <h2>Background:</h2>
  
  <p>I have a Spring 2.5/Java/Tomcat application. There is the following bean, which is used throughout the application in many places</p>
  
  <pre><code>public class HibernateDeviceDao implements DeviceDao
  </code></pre>
  
  <p>and the following bean which is new:</p>
  
  <pre><code>public class JdbcDeviceDao implements DeviceDao
  </code></pre>
  
  <p>The first bean is configured so (all beans in the package are included)</p>
  
  <pre><code>&lt;context:component-scan base-package="com.initech.service.dao.hibernate" /&gt;
  </code></pre>
  
  <p>The second (new) bean is configured separately</p>
  
  <pre><code>&lt;bean id="jdbcDeviceDao" class="com.initech.service.dao.jdbc.JdbcDeviceDao"&gt;
      &lt;property name="dataSource" ref="jdbcDataSource"&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>This results (of course) in an exception when starting the server:</p>
  
  <blockquote>
    <p>nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.sevenp.mobile.samplemgmt.service.dao.DeviceDao] is defined: expected single matching bean but found 2: [deviceDao, jdbcDeviceDao]</p>
  </blockquote>
  
  <p>from a class trying to autowire the bean like this</p>
  
  <pre><code>@Autowired
  private DeviceDao hibernateDevicDao;
  </code></pre>
  
  <p>because there are two beans implementing the same interface. </p>
  
  <h2>The question:</h2>
  
  <p><strong>Is it possible to configure the beans so that</strong> </p>
  
  <p><strong>1.</strong> I don't have to make changes to existing classes, which already have the <code>HibernateDeviceDao</code> autowired</p>
  
  <p><strong>2.</strong> still being able to use the second (new) bean like this:</p>
  
  <pre><code>@Autowired
  @Qualifier("jdbcDeviceDao")
  </code></pre>
  
  <p>I.e. i would need a way to configure the <code>HibernateDeviceDao</code> bean as the default bean to be autowired, simultaneously allowing the usage of a the <code>JdbcDeviceDao</code> when explicitly specifying so with the <code>@Qualifier</code> annotation. </p>
  
  <h2>What I've already tried:</h2>
  
  <p>I tried setting the property</p>
  
  <pre><code>autowire-candidate="false"
  </code></pre>
  
  <p>in the bean configuration for JdbcDeviceDao:</p>
  
  <pre><code>&lt;bean id="jdbcDeviceDao" class="com.initech.service.dao.jdbc.JdbcDeviceDao" autowire-candidate="false"&gt;
      &lt;property name="dataSource" ref="jdbcDataSource"/&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>because the Spring documentation says that</p>
  
  <blockquote>
    <p>Indicates whether or not this bean should be considered when 
     looking for matching candidates to satisfy another bean's 
     autowiring requirements. <strong>Note that this does not affect explicit 
     references by name, which will get resolved even if the specified 
     bean is not marked as an autowire candidate.*</strong></p>
  </blockquote>
  
  <p>which I interpreted to mean that I could still autowire <code>JdbcDeviceDao</code> using the <code>@Qualifier</code> annotation and have the <code>HibernateDeviceDao</code> as default bean. Apparently my interpretation was not correct, though, as this results in the following error message when starting the server:</p>
  
  <blockquote>
    <p>Unsatisfied dependency of type [class com.sevenp.mobile.samplemgmt.service.dao.jdbc.JdbcDeviceDao]: expected at least 1 matching bean</p>
  </blockquote>
  
  <p>coming from the class where I've tried autowiring the bean with a qualifier: </p>
  
  <pre><code>@Autowired
  @Qualifier("jdbcDeviceDao")
  </code></pre>
  
  <h2>Solution:</h2>
  
  <p><a href="https://stackoverflow.com/users/21234/skaffman">skaffman's</a> <a href="https://stackoverflow.com/questions/10534053/autowiring-two-beans-implementing-same-interface-how-to-set-default-bean-to-au#10534137">suggestion</a> to try the @Resource annotation worked. So the configuration has autowire-candidate set to false for jdbcDeviceDao and when using the jdbcDeviceDao I refer to it using the @Resource annotation (instead of @Qualifier):</p>
  
  <pre><code>@Resource(name = "jdbcDeviceDao")
  private JdbcDeviceListItemDao jdbcDeviceDao;
  </code></pre>