报错全称
要求一个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