简体   繁体   中英

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

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)

When I try to access Person object: Person person = ctx.getBean(Person.class); person.showAge();

it throws the following error in Runtime:

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

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.

Does anyone has any clue?

in below, I attached full Error stack and whole source code. Thanks,

the full error is:

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)

Source Code:

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

References: Spring in Action: covers Spring 4 , 4th edition Tutorial Point Spring 3

That's the expected behavior. Spring has no way to know which specific implementation of the Person interface you want, so it throws that nice exception. Ask for the specific implementation, either:

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

or:

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

Or you could also use getBeansOfType() method to retrieve all beans that implement the Person interface:

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

The key would be the name of the bean.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM