报错全称
要求一个Bean,但是发现两个
Parameter 1 of method requestRateLimiterGatewayFilterFactory in org.springframework.cloud.gateway.config.GatewayAutoConfiguration required a single bean, but 2 were found:
代码
两个Bean,或多个Bean,这两个都是通过方法method或者函数创建的,不过无所谓,关键是他们同名了。
@Bean
public KeyResolver userAPIKeyResolver( ) {
@Bean
public KeyResolver ipKeyResolver() {
疑似产生的原因
同名
弯路、坑
分析
没有指定BeanName,Spring使用了默认值,导致重名而不能启动
解决方案
1)在注解增加BeanName配置:@Bean("ipKeyResolver")和@Primary,带@Primary注解的优先,表示是主Bean。消费者使用@Qualifier("name")消费Bean
本文详细解析了在Spring框架中遇到的Bean重名错误,解释了该错误产生的原因,即未指定BeanName导致的Spring默认命名冲突。文章提供了具体的代码示例,并给出了有效的解决方案,包括使用@Bean注解的BeanName属性和@Primary注解来指定主Bean。
1654





