You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SpringBoot启动提示找不到CustomerMapper bean该如何排查解决

问题原因
  • 核心原因:MapStruct注解处理器未正确配置,编译阶段没有生成CustomerMapper接口的实现类。你在@Mapper注解中指定了componentModel = "spring",该配置要求MapStruct生成的实现类自动添加@Component注解,只有实现类正常生成,才会被Spring扫描注入为可用Bean。
  • 次要原因:你贴出的CustomerMapper接口中未声明代码里调用的customerDataToCustomer(CustomerData custData)方法,会导致编译失败,无法生成Mapper实现类。
解决方案

1. 配置正确的MapStruct依赖和注解处理器

根据你使用的构建工具选择对应配置:

Maven配置(pom.xml)

<!-- 依赖配置 -->
<dependencies>
    <dependency>
        <groupId>org.mapstruct</groupId>
        <artifactId>mapstruct</artifactId>
        <version>1.5.5.Final</version> <!-- 替换为你需要的版本 -->
    </dependency>
</dependencies>

<!-- 构建插件配置,添加注解处理器 -->
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
                <source>8</source> <!-- 替换为你项目的JDK版本 -->
                <target>8</target>
                <annotationProcessorPaths>
                    <path>
                        <groupId>org.mapstruct</groupId>
                        <artifactId>mapstruct-processor</artifactId>
                        <version>1.5.5.Final</version> <!-- 和核心包版本保持一致 -->
                    </path>
                </annotationProcessorPaths>
            </configuration>
        </plugin>
    </plugins>
</build>

Gradle配置(build.gradle)

dependencies {
    implementation 'org.mapstruct:mapstruct:1.5.5.Final'
    annotationProcessor 'org.mapstruct:mapstruct-processor:1.5.5.Final'
}

2. 补全Mapper接口的方法声明

补全你业务代码中用到的转换方法,CustomerMapper调整后如下:

package com.application.mapper;

import org.mapstruct.Mapper;
import org.mapstruct.MappingConstants;

@Mapper(componentModel = MappingConstants.ComponentModel.SPRING)
public interface CustomerMapper {
    // 使用Spring组件模型时,不需要手动定义INSTANCE常量,直接注入使用即可
    Customer customerDataToCustomer(CustomerData custData);
    CustomerViewData fromCustToCustViewData(Customer entity);
}

3. 重新编译项目

执行构建工具的清理编译命令:

  • Maven:执行 mvn clean install
  • Gradle:执行 gradle clean build
    编译完成后检查生成的class目录下是否存在CustomerMapperImpl.class实现类,存在即说明生成正常,重启应用即可解决Bean找不到的问题。

注:你当前启动类的@ComponentScan配置已经覆盖了Mapper所在的com.application.mapper包,无需额外调整启动类配置。

内容的提问来源于stack exchange,提问作者ts178

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.12 23:48:04