org.springframework.beans.factory.NoUniqueBeanDefinitionException 异常错误

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_31279347/article/details/82729948
org.springframework.beans.factory.NoUniqueBeanDefinitionException: 
No qualifying bean of type [com.lingnan.pojo.Person] is defined: 
expected single matching bean but found 7: person0,person1,person2,person3,person4,person5,person6
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:313)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:985)
    at com.lingnan.Test0.test1(Test0.java:35)
    at com.lingnan.Test0.main(Test0.java:111)

这个异常是说,一个类配置了多个bean之后,我们还在使用ctx.getBean(Person.class);方法,即根据bean的类映射去获取bean对象。

这个时候返回的bean对象不是唯一的,有多个bean对象。解决方法,就是根据bean的id去获取bean对象。

猜你喜欢

转载自blog.csdn.net/qq_31279347/article/details/82729948