No qualifying bean of type is defined: expected single matching bean but found 2

本文介绍了一个在Spring框架中常见的NoUniqueBeanDefinitionException异常及其解决方法。当试图从Spring容器获取唯一Bean实例但找到多个候选者时会抛出此异常。文章提供了具体的代码示例,并展示了如何通过更详细的@Bean注解来指定所需的Bean,从而避免此类问题。

最近在Spring的注解中,尝试获取一个Bean出现了如题所示的错误。

NoUniqueBeanDefinitionException: No qualifying bean of type is defined: expected single matching bean but found 2:sysRoleManage, sysRoleManageImpl 

 

我有2个类,1个是接口:

 

public interface SysRoleManager{
  //............
}

 另一个是该接口的实现类:

 

 

@Compnent
public class SysRoleManagerImpl implements SysRoleManager{
  //............实现抽象方法
}

 尝试获取bean的时候:

 

 

ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(ServletContext);

SysRoleManager manager = ctx.getBean(SysRoleManager.class);
System.out.println(manager);

 得到如题所示的错误。

 

 

解决办法,在实现类上的注解要更详细些,改成:

 

@Service("sysRoleManager")
public class SysRoleManagerImpl implements SysRoleManager{
  //............实现抽象方法
}

 这样,就能解决问题,打印出:

 

com.xx.xxx.SysRoleManagerImpl@179e64

说明获取到了实现类。

或者可以不用通过Spring上下文去获取,直接透过注解的方式:

@Autowire
@Qualifier("sysRoleManager ")
private SysRoleManager manager;

 

假如有多个实现类实现同一接口,需要在每个实现类上注解不同的@Service("name"),name为不同值。

 

顺便补充下hibernate的一个异常:例如,

org.hibernate.HibernateException: identifier of an instance 

 

of org.cometd.hibernate.User altered from 12 to null。

那是因为:

1)你是否在注解的时候把非主键的字段配置了@NaturalId等主键生成策略;

2)是否在一个事务中更新了主键,而主键是不能被更新的。

 

APPLICATION FAILED TO START *************************** Description: Parameter 0 of method setCacheService in com.tplink.smb.omada.apigateway.dispatch.cache.IpAccessCache required a single bean, but 2 were found: - memCacheService: defined by method 'cacheService' in class path resource [com/tplink/smb/component/cache/mem/MemCacheAutoConfiguration.class] - ohcCacheService: defined by method 'cacheService' in class path resource [com/tplink/smb/component/cache/ohc/OhcCacheAutoConfiguration.class] This may be due to missing parameter name information 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 Ensure that your compiler is configured to use the '-parameters' flag. You may need to update both your build tool settings as well as your IDE. (See https://github.com/spring-projects/spring-framework/wiki/Spring-Framework-6.1-Release-Notes#parameter-name-retention) 10-24-2025 14:39:43.405 ERROR [main] [] c.t.s.o.s.t.SpringBootStartUpTask(): Cannot start up springboot. reson:Error creating bean with name 'shiroFilter' defined in class path resource [com/tplink/smb/omada/apigateway/dispatch/configuration/ShiroFilterConfiguration.class]: Unsatisfied dependency expressed through method 'shiroFilter' parameter 12: Error creating bean with name 'ipAccessRuleFilter': Unsatisfied dependency expressed through method 'setIpAccessCache' parameter 0: Error creating bean with name 'com.tplink.smb.omada.apigateway.dispatch.cache.IpAccessCache': Unsatisfied dependency expressed through method 'setCacheService' parameter 0: No qualifying bean of type 'com.tplink.smb.component.cache.api.CacheService' available: expected single matching bean but found 2: memCacheService,ohcCacheService org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'shiroFilter' defined in class path resource [com/tplink/smb/omada/apigateway/dispatch/configuration/ShiroFilterConfiguration.class]: Unsatisfied dependency expressed through method 'shiroFilter' parameter 12: Error creating bean with name 'ipAccessRuleFilter': Unsatisfied dependency expressed through method 'setIpAccessCache' parameter 0: Error creating bean with name 'com.tplink.smb.omada.apigateway.dispatch.cache.IpAccessCache': Unsatisfied dependency expressed through method 'setCacheService' parameter 0: No qualifying bean of type 'com.tplink.smb.component.cache.api.CacheService' available: expected single matching bean but found 2: memCacheService,ohcCacheService 这个报错怎么解决
最新发布
10-25
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值