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

相关文章
|
XML C# 数据格式
C#报错 The ‘xmins‘ attribute is not supported in this context
System.Xml.Schema.XmlSchemaException:“The ‘xmins’ attribute is not supported in this context.”异常的解决百度翻译:System.Xml.Schema.XmlSchemaException:“此上下文中不支持”xmins“属性。”这是在使用System.Data.DataSet的ReadXml读取x...
76 0
C#报错 The ‘xmins‘ attribute is not supported in this context
|
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
203 0
|
3月前
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
94 3
|
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
99 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.
215 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
|
Java
The type XXX cannot be resolved. It is indirectly referenced from required .class files
The type XXX cannot be resolved. It is indirectly referenced from required .class files
128 0
解决办法:Type safety: The expression of type List needs unchecked conversion to conform
解决办法:Type safety: The expression of type List needs unchecked conversion to conform
320 0