required a single bean, but 2 were found:

在学习使用 mybatis-plus 时,遇到一个奇怪的异常 如 代码一:

***************************
APPLICATION FAILED TO START
***************************

Description:

Field baseMapper in com.baomidou.mybatisplus.extension.service.impl.ServiceImpl required a single bean, but 2 were found:
	- sysUserMapper: defined in file [/Users/wzengheng/linewell/wis-mom-platform/wis-organization/wis-organization-microservice/target/classes/com/wis/organization/infrastructure/persistence/dao/SysUserMapper.class]
	- sysUserRoleMapper: defined in file [/Users/wzengheng/linewell/wis-mom-platform/wis-organization/wis-organization-microservice/target/classes/com/wis/organization/infrastructure/persistence/dao/SysUserRoleMapper.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

Disconnected from the target VM, address: '127.0.0.1:61021', transport: 'socket'

会出现这种问题的原因是在使用Mybatis-plus的时候 xxxService要实现IService接口,xxxxx要继承ServiceImpl<xxxxMapper, xxxx>,   xxxxMapper  要继承BaseMapper接口,因此在进行注入的时候Spring区分不了要注入哪一个,因为每个mapper都要去继承BaseMapper。最简单的方法就是放弃使用Mybatis-plus 去使用Mybatis,这样是能正常使用的,但是这不是我想要的,所以下面是我解决这个问题的方法。

在碰到这种问题的时候网上也查找了很多的方法,但是都没有用,我是在实体类当中,使用工厂类来获取仓储的bean,我是这样子定义的

  /**
     * 
     */
    private static SysUserReference sysUserReference = RepoFactory.get(SysUserReference.class);

    /**
     * 
     */
    private static SysUserRoleReference sysUserRoleReference = RepoFactory.get(SysUserRoleReference.class);

 

定义公用的bean,这时候就会报这种错误,我将代码修改成下面这种要用到在去获取的方式  就能正常使用了,另外还有一个问题就是 实体类当中 不能使用@Autowired来进行注入,否则会报空指针异常。切记切记

    /**
     * 获取
     *
     * @return
     */
    public Page<SysUser> selectList() {
        return RepoFactory.get(SysReference.class).selectList(this.obtainPage(), this);
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
"required a single bean, but 2 were found"是Spring框架中的一个错误,意味着在注入依赖关系时发现了多个匹配的Bean。这个错误通常发生在使用@Autowired注解时,Spring无法确定要注入哪个具体的Bean实例。 该错误通常的原因是在应用程序上下文中存在多个与注入的类型匹配的Bean。这可能是因为在配置文件中或使用注解时定义了多个相同类型的Bean。 解决这个问题的方法之一是使用@Qualifier注解来指定唯一的Bean。@Qualifier注解与@Autowired注解一起使用,它允许精确指定要注入的Bean的名称或ID。通过在@Autowired注解中使用@Qualifier注解,可以确保只有一个Bean被注入。 另一种方法是使用@Primary注解来指定主要的Bean。当有多个匹配的Bean时,Spring会优先选择带有@Primary注解的Bean进行注入。通过使用@Primary注解,可以解决"required a single bean, but 2 were found"的错误。 总结起来,当出现"required a single bean, but 2 were found"的错误时,可以通过使用@Qualifier注解或@Primary注解来指定具体的Bean,解决注入多个匹配Bean的问题。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [解决 required a single bean, but 2 were found的spring注入bean错误](https://blog.csdn.net/qq_33333654/article/details/121627052)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [@Autowired注解 --required a single bean, but 2 were found出现的原因以及解决方法](https://blog.csdn.net/qq_43479892/article/details/126264223)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值