org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'com.ps.test.Person' available: expected single matching bean but found 2: com.ps.test.Person#0,com.ps.test.Person#1
分析: 这个错误是因为在获取Bean
时是按类型获取的,而此时spring容器中有多个同类型的对象而导致的.
错误代码:
ApplicationContext ac= new ClassPathXmlApplicationContext("beans.xml");
Person bean = ac.getBean(Person.class); //按类型获取Bean
System.out.println(bean);
解决:
按照id
获取Bean
即可.
示范代码:
ApplicationContext ac= new ClassPathXmlApplicationContext("beans.xml");
Person bean = ac.getBean("person",Person.class); //按Id获取Bean
System.out.println(bean);