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

时间:2015-03-02 16:08:39

标签: java spring interface autowired ambiguity

我正在练习春季4;我有一个接口(人),其中有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和/或@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 in Action:涵盖Spring 4,4th edition 教程点Spring 3

1 个答案:

答案 0 :(得分:0)

这是预期的行为。 Spring无法知道你想要的Person接口的具体实现,所以它抛出了那个不错的异常。要求具体实施,可以:

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

或:

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

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

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

密钥将是bean的名称。