title: Exception org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type is defined: expected single matching bean
body: <p>I am practicing Spring 4; I have an Interface (Person) with 2 its implementer classes (Professor and Student). the configuration is Automatic with Java (AutomaticBeanConfiguration.java)</p>
  
  <p>When I try to access Person object:
   Person person = ctx.getBean(Person.class);
   person.showAge();</p>
  
  <p>it throws the following error in Runtime:</p>
  
  <pre><code>Exception in thread "main" org.springframework.beans.factory.NoUniqueBeanDefinitionException:
      No qualifying bean of type [spring.automaticconfiguration.ambiguityautowiring.solution.qualifier.practice2.Person] is defined:
      expected single matching bean but found 2: student,professor
  </code></pre>
  
  <p>I know that @Primary and/or @Qualifier should resolve the issue, but since it is automatic java  configuration, I couldn't find the right solution.</p>
  
  <p>Does anyone has any clue?</p>
  
  <h2>in below, I attached full Error stack and whole source code. Thanks,</h2>
  
  <p>the full error is:</p>
  
  <pre><code>Mar 02, 2015 10:36:40 AM org.springframework.context.support.AbstractApplicationContext prepareRefresh
  INFO: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@115c6cb: startup date [Mon Mar 02 10:36:40 EST 2015]; root of context hierarchy
  Exception in thread "main" org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [spring.automaticconfiguration.ambiguityautowiring.solution.qualifier.practice2.Person] is defined: expected single matching bean but found 2: student,professor
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:365)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:331)
      at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:968)
      at spring.automaticconfiguration.ambiguityautowiring.solution.qualifier.practice2.AppMain.main(AppMain.java:44)
  </code></pre>
  
  <hr>
  
  <p>Source Code:</p>
  
  <pre><code>public interface Person {
      public String showName();
  
      public Integer showAge();
  }
  </code></pre>
  
  <hr>
  
  <pre><code>import org.springframework.beans.factory.annotation.Qualifier;
  import org.springframework.stereotype.Component;
  
  @Component
  @Qualifier("professor")
  public class Professor implements Person {
      private String name;
      private Integer age;
      public String getName() {
          return name;
      }
      public void setName(String name) {
          this.name = name;
      }
      public Integer getAge() {
          return age;
      }
      public void setAge(Integer age) {
          this.age = age;
      }
      public String showName() {
          return getName();
      }
      public Integer showAge() {
          return getAge();
      }
  
  }
  </code></pre>
  
  <hr>
  
  <pre><code>import org.springframework.beans.factory.annotation.Qualifier;
  import org.springframework.stereotype.Component;
  
  @Component
  @Qualifier("student")
  public class Student implements Person {
      private String name;
      private Integer age;
      public String getName() {
          return name;
      }
      public void setName(String name) {
          this.name = name;
      }
      public Integer getAge() {
          return age;
      }
      public void setAge(Integer age) {
          this.age = age;
      }
      public String showName() {
          return getName();
      }
      public Integer showAge() {
          return getAge();
      }
  }
  </code></pre>
  
  <hr>
  
  <pre><code>import org.springframework.beans.factory.annotation.Qualifier;
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.ComponentScan;
  import org.springframework.context.annotation.Configuration;
  
  @Configuration
  @ComponentScan //For Automatic Bean Discovery aby Spring Framework
  public class AutomaticBeanConfiguration {
  }
  </code></pre>
  
  <hr>
  
  <pre><code>import org.springframework.context.ApplicationContext;
  import org.springframework.context.annotation.AnnotationConfigApplicationContext;
  
  public class AppMain {
      public static void main(String[] args) {
          ApplicationContext ctx = new AnnotationConfigApplicationContext(
                  AutomaticBeanConfiguration.class);
  
         //Doesn't work
         // expected single matching bean but found 2: student,professor
  
          Person person = ctx.getBean(Person.class);
          person.showAge();
      }
  }
  </code></pre>
  
  <p>References:
  Spring in Action: covers Spring 4  , 4th edition
  Tutorial Point Spring 3</p>