SpringBoot Error:XXBean required a single bean, but 2 were found:


APPLICATION FAILED TO START


Description:

Field userbody in com.example.demo.controller.HelloControllerBean required a single bean, but 2 were found:
- userBody: defined in file [C:\Users\Administrator\IdeaProjects\demo\target\classes\com\example\demo\bean\UserBody.class]
- userbody-com.example.demo.bean.UserBody: defined in null

在使用Spring框架中@Autowired标签时默认情况下使用 @Autowired 注释进行自动注入时,Spring 容器中匹配的候选 Bean 数目必须有且仅有一个。当找不到一个匹配的 Bean 时,Spring 容器将抛BeanCreationException 异常,并指出必须至少拥有一个匹配的 Bean。

其实SpringBoot已经给出解决方案了
Action:
Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed

解决方案:
1.在实体类前面加上@primary的标签
在这里插入图片描述
2.给Autowired注入的对象添加@Qualifier注解

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值