解决required a single bean, but 2 were found问题

简介: 背景:springboot整合shiro中自定义Realm时出现错误描述Parameter 0 of method getDefaultWebSecurityManager in cn.ken.springboot_shiro.config.ShiroConfig required a single bean, but 2 were foun

背景:springboot整合shiro中自定义Realm时出现


错误描述


Parameter 0 of method getDefaultWebSecurityManager in cn.ken.springboot_shiro.config.ShiroConfig required a single bean, but 2 were found:


getRealm: defined by method ‘getRealm’ in class path resource [cn/ken/springboot_shiro/config/ShiroConfig.class]

iniClasspathRealm: defined by method ‘iniClasspathRealm’ in class path resource [org/apache/shiro/spring/boot/autoconfigure/ShiroAutoConfiguration.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

场景再现

// 创建安全管理器
@Bean
public DefaultWebSecurityManager getDefaultWebSecurityManager(Realm realm){
    DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();
    // 给安全管理器设置realm
    defaultWebSecurityManager.setRealm(realm);
    return defaultWebSecurityManager;
}

错误分析:容器中出现了两个Realm类型的Bean,注入时不知道选择哪一个

解决方法:在参数中注入Realm时指定bean的id以区分

// 创建安全管理器
@Bean
public DefaultWebSecurityManager getDefaultWebSecurityManager(@Qualifier("getRealm") Realm realm){
    DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();
    // 给安全管理器设置realm
    defaultWebSecurityManager.setRealm(realm);
    return defaultWebSecurityManager;
}
// 创建自定义Realm
@Bean
public Realm getRealm(){
    return new CustomerRealm();
}

@Qualifier(“getRealm”)参数之所以为getRealm是因为下面@Bean的方法名为getRealm()

相关文章
IDEA添加Swagger2:Parameter 0 of method linkDiscoverers in org. springframework hateoas.config.Hateoasconfiguration required a single bean, but 15 were found:
IDEA添加Swagger2:Parameter 0 of method linkDiscoverers in org. springframework hateoas.config.Hateoasconfiguration required a single bean, but 15 were found
|
6月前
|
XML 数据格式
Could not autowire. No beans of ‘VideoDao‘ type found.
Could not autowire. No beans of ‘VideoDao‘ type found.
|
Java 数据库连接 Redis
Bean method ‘redisConnectionFactory‘ not loaded because @ConditionalOnClass did not find required c
Bean method ‘redisConnectionFactory‘ not loaded because @ConditionalOnClass did not find required c
126 0
|
NoSQL Java Redis
5. 成功解决:Could not autowire. No beans of 'RedisConnectionFactory' type found.
今天建了一个新项目,使用 Spring Boot 整合 Redis 时,IDEA 被提醒“Could not autowire. No beans of 'RedisConnectionFactory' type found. ”错误,意思是不能自动装配“RedisConnectionFactory”。
3345 1
|
JSON 数据格式
Required request parameter ‘name‘ for method parameter type String is not present 报错解决方法
Required request parameter ‘name‘ for method parameter type String is not present 报错解决方法
3846 0
使用pageHelper报错 Type definition error: [simple type, classXXXX]
使用pageHelper报错 Type definition error: [simple type, classXXXX]
|
Linux
ERROR: 2 matches found based on name: network product-server_default is ambiguous
ERROR: 2 matches found based on name: network product-server_default is ambiguous
170 0
|
druid
A bean with that name has already been defined in class path resource and overriding is disabled.
A bean with that name has already been defined in class path resource and overriding is disabled.
236 0
Optional int parameter ‘id‘ is present but cannot be translated into a null value due to being ……
Optional int parameter ‘id‘ is present but cannot be translated into a null value due to being ……
333 0
|
Java API Spring
A component required a bean named xxx that could not be found
A component required a bean named xxx that could not be found
A component required a bean named xxx that could not be found

热门文章

最新文章