lundi 2 mars 2015

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


Aucun commentaire:

Enregistrer un commentaire