
本文旨在解决 Spring Boot 项目中使用 MapStruct 时遇到的 `Autowired` 注入失败问题。我们将分析导致该问题的常见原因,并提供详细的排查步骤和解决方案,确保 MapStruct 能够正确生成映射器 Bean 并注入到 Spring 容器中。
在 Spring Boot 项目中,当使用 MapStruct 进行对象映射时,可能会遇到类似如下的错误:
Description: Parameter 1 of constructor in api.loteria.loteriaapi.services.Mysql.BetServiceMysql required a bean of type 'api.loteria.loteriaapi.dtos.mappers.BetMapper' that could not be found.
这个错误表明 Spring 容器无法找到 BetMapper 接口的 Bean 实例,导致无法注入到 BetServiceMysql 中。这通常是由于以下原因造成的:
针对上述可能的原因,我们可以采取以下步骤进行排查和解决:
确保在 pom.xml 或 build.gradle 文件中正确配置了 mapstruct-processor 插件。
Maven (pom.xml):
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>1.5.5.Final</version>
</path>
<!-- other annotation processors -->
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>1.5.5.Final</version>
</dependency>
<!-- other dependencies -->
</dependencies>Gradle (build.gradle):
dependencies {
implementation 'org.mapstruct:mapstruct:1.5.5.Final'
annotationProcessor 'org.mapstruct:mapstruct-processor:1.5.5.Final'
// For Java 9 and later, you might need to add the following:
annotationProcessor "org.projectlombok:lombok"
compileOnly "org.projectlombok:lombok"
testImplementation "org.projectlombok:lombok"
testAnnotationProcessor "org.projectlombok:lombok"
}注意:
确保 Mapper 接口使用了 @Mapper 注解,并且 componentModel 属性设置为 "spring"。
@Mapper(componentModel = "spring")
public interface BetMapper {
@Mapping(target = "id", source = "betRequest.betId") // 修改了 mapping
Bet betResquetToEntity(BetRequest betRequest);
@Mapping(source = "id", target = "betId") // 修改了 mapping
BetResponse entityToBetResponse(Bet bet);
}注意:
在修改了配置后,需要清理和重新构建项目,以确保 MapStruct 处理器能够正确生成映射器的实现类。
使用 Maven 或 Gradle 的依赖分析工具,检查是否存在与 MapStruct 相关的依赖冲突。如果存在冲突,需要解决冲突,确保 MapStruct 的版本正确。
确保 IDE 正确配置了 annotation processing。
在 BetServiceMysql 中,确保 BetMapper 使用构造器注入,并且 @AllArgsConstructor 注解正确配置。
@Service
@AllArgsConstructor(onConstructor_ = @Autowired) // 确保Autowired注解在构造器上
public class BetServiceMysql implements BetService {
private BetRepository betRepository;
private BetMapper betMapper;
// ...
}注意:
如果移除了 @Autowired 注解后,代码可以运行,但是运行时 Mapper 为 Null,这说明 MapStruct 处理器没有正确生成 Mapper 的实现类,或者 Spring 容器没有正确管理 Mapper 的 Bean。 请仔细检查上述步骤,特别是 MapStruct 处理器的配置和 Mapper 接口的配置。
解决 Spring Boot 中 MapStruct 注入失败的问题,需要仔细检查 MapStruct 处理器的配置、Mapper 接口的配置、依赖冲突和 IDE 配置。 通过以上步骤,可以确保 MapStruct 能够正确生成映射器的实现类,并将其注入到 Spring 容器中。 如果问题仍然存在,请提供更详细的错误信息和配置信息,以便更好地进行分析和解决。
以上就是Spring Boot 中 MapStruct 注入失败问题排查与解决的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号