异常org.springframework.beans.factory.NoUniqueBeanDefinitionException:没有定义类型的限定bean:预期的单个匹配bean

Exception org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type is defined: expected single matching bean

我正在练习Spring4;我有一个接口(人)和 2 个它的实现者 类(教授和学生)。配置是自动的 Java (AutomaticBeanConfiguration.java)

当我尝试访问 Person 对象时: Person person = ctx.getBean(Person.class); person.showAge();

它在运行时抛出以下错误:

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

我知道@Primary and/or @Qualifier 应该可以解决这个问题,但由于它是自动 java 配置,我找不到正确的解决方案。

有人知道吗?

在下面,我附上了完整的错误堆栈和完整的源代码。谢谢,

完整的错误是:

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)

源代码:

public interface Person {
    public String showName();

    public Integer showAge();
}

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();
    }

}

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();
    }
}

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 {
}

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();
    }
}

参考资料: Spring 实战:涵盖 Spring 4,第 4 版 辅导点 Spring 3

这是预期的行为。 Spring 无法知道您想要 Person 接口的哪个特定实现,因此它会抛出那个很好的异常。求具体实现,要么:

Person person = ctx.getBean(Student.class);

或:

Person person = ctx.getBean(Professor.class);

或者您也可以使用 getBeansOfType() method 检索所有实现 Person 接口的 beans:

Map<String, Person> persons = ctx.getBeansOfType(Person.class);

键是 bean 的名称。