SpringBoot启动报错 【xx required a single bean, but 2 were found】


APPLICATION FAILED TO START


Description:

Field registration in org.springframework.cloud.client.serviceregistry.ServiceRegistryAutoConfiguration$ServiceRegistryEndpointConfiguration required a single bean, but 2 were found:
- nacosRegistration: defined by method ‘nacosRegistration’ in class path resource [org/springframework/cloud/alibaba/nacos/NacosDiscoveryAutoConfiguration.class]
- eurekaRegistration: defined by method ‘eurekaRegistration’ in class path resource [org/springframework/cloud/netflix/eureka/EurekaClientAutoConfiguration.class]

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

Disconnected from the target VM, address: ‘127.0.0.1:61444’, transport: ‘socket’

Process finished with exit code 1
在这里插入图片描述

解决办法:
排除其中一个自动配置,例如上面是NacosDiscoveryAutoConfiguration 和 EurekaClientAutoConfiguration。这里排除EurekaClientAutoConfiguration。

@SpringBootApplication(exclude = EurekaClientAutoConfiguration.class)
效果如下,可以正常启动:
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
"required a single bean, but 2 were found"是Spring框架中的一个错误,意味着在注入依赖关系时发现了多个匹配的Bean。这个错误通常发生在使用@Autowired注解时,Spring无法确定要注入哪个具体的Bean实例。 该错误通常的原因是在应用程序上下文中存在多个与注入的类型匹配的Bean。这可能是因为在配置文件中或使用注解时定义了多个相同类型的Bean。 解决这个问题的方法之一是使用@Qualifier注解来指定唯一的Bean。@Qualifier注解与@Autowired注解一起使用,它允许精确指定要注入的Bean的名称或ID。通过在@Autowired注解中使用@Qualifier注解,可以确保只有一个Bean被注入。 另一种方法是使用@Primary注解来指定主要的Bean。当有多个匹配的Bean时,Spring会优先选择带有@Primary注解的Bean进行注入。通过使用@Primary注解,可以解决"required a single bean, but 2 were found"的错误。 总结起来,当出现"required a single bean, but 2 were found"的错误时,可以通过使用@Qualifier注解或@Primary注解来指定具体的Bean,解决注入多个匹配Bean的问题。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [解决 required a single bean, but 2 were found的spring注入bean错误](https://blog.csdn.net/qq_33333654/article/details/121627052)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [@Autowired注解 --required a single bean, but 2 were found出现的原因以及解决方法](https://blog.csdn.net/qq_43479892/article/details/126264223)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值