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注解

在这里插入图片描述

你可能感兴趣的:(SpringBoot Error:XXBean required a single bean, but 2 were found:)