解决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()

相关文章
|
Java 数据库连接 mybatis
Consider defining a bean of type ‘com.example.democrud.democurd.usermapper.DaoMapper‘ in your config
Consider defining a bean of type ‘com.example.democrud.democurd.usermapper.DaoMapper‘ in your config
224 0
|
5月前
Error creating bean with name 'eurekaAutoServiceRegistration': Singleton bean creation not allowed while singletons
Error creating bean with name 'eurekaAutoServiceRegistration': Singleton bean creation not allowed while singletons
131 3
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
|
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
134 0
|
Java 数据库连接 mybatis
mybatis报错:Type handler was null on parameter mapping or property ‘__frch_xxx_0’
mybatis报错:Type handler was null on parameter mapping or property ‘__frch_xxx_0’
2147 0
mybatis报错:Type handler was null on parameter mapping or property ‘__frch_xxx_0’
|
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 报错解决方法
3870 0
使用pageHelper报错 Type definition error: [simple type, classXXXX]
使用pageHelper报错 Type definition error: [simple type, classXXXX]
|
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.
238 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 ……
336 0

热门文章

最新文章