通过源码解决Field xxxMapper in xxx required a bean of type ‘xxxMapper‘ that could not be found.

序言

项目中我们经常会遇到这样的错误:Field userMapper in com.shiqi.mybatis.bug.demo.service.impl.UserServiceImpl required a bean of type 'com.shiqi.mybatis.bug.demo.mapper.UserMapper' that could not be found.
今天我们就一起通过源码来探究下产生的原因。

源码探究

SpringBoot 的核心就是自动装配,所以MyBatis整合Spring Boot的话,一定也会提供一个MyBatisAutoConfiguration

我们进行全局搜索找到MyBatisAutoConfiguration这个配置类,然后会发现这个类中有两个内部类:

AutoConfiguredMapperScannerRegistrar

在这里插入图片描述
通过注释我们可以看到MyBatis扫描的路径为Spring Boot启动类同级下的包。
在这里插入图片描述
我们往下看,会发现扫描路径下使用@Mapper注解的接口
在这里插入图片描述

所以上述Bug, 我们的解决方案就是在Mapper接口上加上@Mapper注解。

⚠️ 但是尤其注意的是,如果Mapper接口在不同的Maven模块中,即使使用了@Mapper 注解进行标识,也不不能扫描到。

MapperScannerRegistrarNotFoundConfiguration

在这里插入图片描述
这个内部类允许我们的mapper接口不在Spring Boot启动类同级包路径下。
在这里插入图片描述

而这个内部类只有在没有MapperFactoryBeanMapperScanConfigure两个类的时候生效。
在这里插入图片描述

所以如果项目是分多模块的并且Mapper接口不和SpringBoot 启动类在同一级目录下,我们通过@MapperScan注解指定扫描的路径为Mapper接口所在的包。

可以看看游艺同学的分析
MyBatis常见问题分析!源码跟踪技巧!看完你就能找到那些消失的Mapper了!

Parameter 2 of constructor in org.apache.dolphinscheduler.server.worker.executor.PhysicalTaskExecutorFactory required a bean of type 'org.apache.dolphinscheduler.plugin.storage.api.StorageOperator' that could not be found. The injection point has the following annotations: - @org.springframework.beans.factory.annotation.Autowired(required=true) The following candidates were found but could not be injected: - User-defined bean method 'storageOperate' in 'StorageConfiguration' ignored as the bean value is null Action: Consider revisiting the entries above or defining a bean of type 'org.apache.dolphinscheduler.plugin.storage.api.StorageOperator' in your configuration. [WI-0][TI-0] - 2025-09-24 09:04:05.770 ERROR [Worker-Server] o.a.d.c.t.DefaultUncaughtExceptionHandler:[44] - Caught an exception in Thread[Worker-Server,5,main]. org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'workerServer': Unsatisfied dependency expressed through field 'physicalTaskEngineDelegator'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'physicalTaskEngineDelegator' defined in URL [jar:file:/opt/dolphinscheduler/libs/dolphinscheduler-worker-3.3.1.jar!/org/apache/dolphinscheduler/server/worker/executor/PhysicalTaskEngineDelegator.class]: Unsatisfied dependency expressed through constructor parameter 1; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'physicalTaskExecutorFactory' defined in URL [jar:file:/opt/dolphinscheduler/libs/dolphinscheduler-worker-3.3.1.jar!/org/apache/dolphinscheduler/server/worker/executor/PhysicalTaskExecutorFactory.class]: Unsatisfied dependency expressed through constructor parameter 2; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.apache.dolphinscheduler.plugin.storage.api.StorageOperator' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
最新发布
09-25
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值