title: Does Spring @Autowired inject beans by name or by type?
body: <p>I am reading beginning spring (wiley press) book. In chapter 2 there is an example
  about Java configuration and <code>@Autowired</code>. It provides this <code>@Configuration</code> class</p>
  
  <pre><code>@Configuration
  public class Ch2BeanConfiguration {
  
      @Bean
      public AccountService accountService() {
          AccountServiceImpl bean = new AccountServiceImpl();
          return bean;
      }
  
      @Bean
      public AccountDao accountDao() {
          AccountDaoInMemoryImpl bean = new AccountDaoInMemoryImpl();
          //depedencies of accountDao bean will be injected here...
          return bean;
      }
  
      @Bean
      public AccountDao accountDaoJdbc() {
          AccountDaoJdbcImpl bean = new AccountDaoJdbcImpl();
          return bean;
      }
  }
  </code></pre>
  
  <p>and this regular bean class</p>
  
  <pre><code>public class AccountServiceImpl implements AccountService {
  
      @Autowired
      private AccountDao accountDao;
  
      public void setAccountDao(AccountDao accountDao) {
          this.accountDao = accountDao;
      }
      ...
  }
  </code></pre>
  
  <p>When I run the code, it works. But I expected an exception because I have defined 2 beans with the same type in the configuration. </p>
  
  <p>I realized it works like this:</p>
  
  <ul>
  <li>if Spring encounters multiple beans with same type it checks field name.
  <ul>
  <li>if it finds a bean with the name of the target field, it injects that bean into the field.</li>
  </ul></li>
  </ul>
  
  <p>Isn't this wrong? Is there a bug in Spring's handling of Java configuration? </p>