Spring Boot 3无法识别MapStruct生成的@Component实现类问题求助
Spring Boot 3 无法注入MapStruct映射器接口问题
核心问题
已通过MapStruct生成带有@Component注解的MyMapperImpl实现类,且确认该Bean存在于Spring应用上下文,但注入MyMapper接口时,Spring提示找不到对应类型的Bean。执行构建测试或启动应用时触发错误,跳过测试可正常完成构建。
已验证的配置与状态
Maven编译插件配置(已正确引入MapStruct注解处理器)
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>${java.version}</source> <target>${java.version}</target> <annotationProcessorPaths> <path> <groupId>org.mapstruct</groupId> <artifactId>mapstruct-processor</artifactId> <version>${mapstruct.version}</version> </path> </annotationProcessorPaths> </configuration> </plugin>
生成的Mapper实现类
- 生成位置:
target/generated-sources/annotations目录 - 代码片段:
package com.example.demo; import javax.annotation.processing.Generated; import org.springframework.stereotype.Component; @Generated( value = "org.mapstruct.ap.MappingProcessor", date = "2025-04-12T09:58:09+0000", comments = "version: 1.6.3, compiler: javac, environment: Java 21.0.5" ) @Component public class MyMapperImpl implements MyMapper { @Override // 映射逻辑实现 }
错误日志
2025-04-12T09:58:45.338Z WARN 12073 --- [demo] [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'demoController' defined in file [/workspaces/spring/mapstruct/target/classes/com/example/demo/DemoController.class]: Unsatisfied dependency expressed through constructor parameter 0: No qualifying bean of type 'com.example.demo.MyMapper' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {} 2025-04-12T09:58:45.340Z INFO 12073 --- [demo] [ main] o.apache.catalina.core.StandardService : Stopping service [Tomcat] 2025-04-12T09:58:45.346Z INFO 12073 --- [demo] [ main] .s.b.a.l.ConditionEvaluationReportLogger : Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled. 2025-04-12T09:58:45.363Z ERROR 12073 --- [demo] [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPLICATION FAILED TO START *************************** Description: Parameter 0 of constructor in com.example.demo.DemoController required a bean of type 'com.example.demo.MyMapper' that could not be found. Action: Consider defining a bean of type 'com.example.demo.MyMapper' in your configuration.
复现步骤
./mvnw clean install # 执行测试时触发错误 ./mvnw clean install -DskipTests=true # 跳过测试可成功构建 ./mvnw spring-boot:run # 启动应用时触发错误
环境信息
- JDK版本:21
- 运行环境:VSCode Java开发容器
- 补充排查:通过Spring日志确认应用上下文存在
myMapperImplBean,但Spring无法将其识别为MyMapper接口类型的Bean
内容的提问来源于stack exchange,提问作者chorlton
相关产品推荐
相关产品推荐

