org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'com.atguigu.dao.BookDao' available: expected single matching bean but found 2: bookDao,bookDao2 at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveNamedBean(DefaultListableBeanFactory.java:1180) at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveBean(DefaultListableBeanFactory.java:416) at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:349) at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:342) at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1126) at com.atguigu.test.IOCTest_Autowired.test01(IOCTest_Autowired.java:30) at java.util.ArrayList.forEach(ArrayList.java:1257) at java.util.ArrayList.forEach(ArrayList.java:1257)
解决:
在其中一个 bean 上加 @Primary,使得自动配置时不报错。
@Primary 让Spring自动装配的时候,默认使用装配首选的bean
根据 bean 的 id 去获取 bean 对象。
org.springframework.beans.factory.NoUniqueBeanDefinitionException 异常
最新推荐文章于 2024-09-15 10:09:10 发布