【Spring Boot 多模块项目】@MapperScan失效、MapperScannerConfigurer 报错终极解决方案

在使用 Spring Boot 构建多模块项目,集成 MyBatis-Plus 时,很多开发者会遇到类似如下启动报错:

复制代码
Error creating bean with name 'mapperScannerConfigurer' ...
Caused by: java.lang.IllegalArgumentException: Property 'basePackage' is required

即使在启动类上正确添加了 @MapperScan("com.xxx.mapper"),项目依然报错无法启动。这到底是怎么回事?我们该如何优雅地配置 Mapper 扫描?本文将逐步分析问题成因,最后给出推荐方案。


一、问题背景

项目结构如下(典型的多模块结构):

复制代码
parent
├── common              # 公共模块,封装配置类等
├── user                # 业务模块,包含启动类、Mapper 接口
└── pom.xml

user 模块中有如下启动类:

java 复制代码
@SpringBootApplication
@MapperScan("com.partner.user.mapper")
public class UserApplication {
    public static void main(String[] args) {
        SpringApplication.run(UserApplication.class, args);
    }
}

common 模块中配置了如下 MybatisPlusConfig

java 复制代码
@Configuration
@EnableConfigurationProperties(MyBatisPlusProperties.class)
public class MybatisPlusConfig {

    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer(MyBatisPlusProperties myBatisPlusProperties) {
        System.out.println("Mapper scan package: " + myBatisPlusProperties.getMapperScan());
        MapperScannerConfigurer configurer = new MapperScannerConfigurer();
        configurer.setBasePackage(myBatisPlusProperties.getMapperScan());
        return configurer;
    }
}

配置类依赖的配置文件如下(写在 user 模块的 bootstrap.yml):

复制代码
mybatis:
  mapperScan: com.partner.user.mapper

二、项目启动失败的根本原因

启动失败栈信息:

复制代码
Caused by: java.lang.IllegalArgumentException: Property 'basePackage' is required

也就是说:MapperScannerConfigurer 没有成功读取到 basePackage,传入的是 null 。从而导致 afterPropertiesSet() 抛出异常。

根本原因是:

MapperScannerConfigurer 是一个 Spring Bean,提前初始化,但它依赖的 mybatis.mapperScan 配置却定义在 bootstrap.yml 中,属于 Spring Cloud 的配置生命周期,尚未加载完成,导致值为 null


三、常见误区总结

错误实践 原因
配置 MapperScannerConfigurer 并从 bootstrap.yml 读取 mapperScan 配置未加载时就被调用,null 值引发异常
在多个模块分别配置 mapper 扫描路径 不同模块配置加载顺序不确定,容易互相覆盖或无效
同时使用 @MapperScanMapperScannerConfigurer 存在重复扫描、不一致行为风险

四、推荐解决方案

✅ 推荐做法:只使用 @MapperScan 注解,彻底抛弃 MapperScannerConfigurer

这是最简洁、最稳定的做法,推荐如下方式:

1. 在启动类中添加注解:
java 复制代码
@SpringBootApplication
@MapperScan("com.partner.user.mapper") // 替换为你自己的 mapper 包路径
public class UserApplication {
    public static void main(String[] args) {
        SpringApplication.run(UserApplication.class, args);
    }
}
2. 删除 MapperScannerConfigurer 相关 Bean:
java 复制代码
// ❌ 删除该 Bean,否则仍然会因配置未加载而报错
@Bean
public MapperScannerConfigurer mapperScannerConfigurer(...) {
    ...
}

这样一来,MyBatis-Plus 会根据注解自动扫描 Mapper 接口,避免复杂配置和潜在异常。


五、其他可选做法(不推荐)

如果你确实有特殊需求,想动态配置扫描路径,也应注意以下几点:

  1. 配置写在 application.yml 而非 bootstrap.yml,确保其生命周期早于 Bean 初始化;

  2. 或使用 Spring Boot 2.2+ 的 @ConfigurationPropertiesScan 自动扫描配置类

示例:

java 复制代码
@ConfigurationProperties(prefix = "mybatis")
public class MyBatisPlusProperties {
    private String mapperScan;
}

@Configuration
@ConfigurationPropertiesScan("com.partner.common.config")
public class MybatisPlusConfig {
    ...
}

六、总结建议

方案 是否推荐 说明
@MapperScan("xxx") 注解 ✅ 推荐 简单高效,避免生命周期问题
自定义配置类读取扫描包路径 ❌ 不推荐 配置不易统一,生命周期难控制
多模块共用一个配置类 + 注解扫描 ✅ 推荐 公共模块做分页拦截器等配置,Mapper 扫描交给启动类注解

七、写在最后

在实际开发中,越是多模块复杂项目,越应该避免不必要的配置项和手动扫描逻辑。优雅使用 @MapperScan 是最稳定的 MyBatis 配置方案。

如果你也遇到了类似的问题,希望这篇文章能帮你少走弯路!


📌如果本文对你有帮助,欢迎点赞、收藏、评论支持一下!

📮如有更多 Spring Boot 多模块踩坑问题,欢迎留言交流~

相关推荐
Duang007_7 小时前
【万字学习总结】API设计与接口开发实战指南
开发语言·javascript·人工智能·python·学习
小北方城市网7 小时前
JVM 调优实战指南:从问题排查到参数优化
java·spring boot·python·rabbitmq·java-rabbitmq·数据库架构
一叶星殇7 小时前
C# .NET 如何解决跨域(CORS)
开发语言·前端·c#·.net
Elieal7 小时前
Java项目密码加密实现详解
java·开发语言
shhpeng7 小时前
go mod vendor命令详解
开发语言·后端·golang
Java程序员威哥7 小时前
用Java玩转机器学习:协同过滤算法实战(比Python快3倍的工程实现)
java·开发语言·后端·python·算法·spring·机器学习
GeekyGuru7 小时前
C++跨平台开发的核心挑战与应对策略
开发语言·c++
牧小七7 小时前
java StampedLock 的使用
java
Howrun7777 小时前
信号量(Semaphore)
开发语言·c++·算法
leaves falling7 小时前
c语言-动态内存管理
c语言·开发语言