MapStruct类型转换接口未自动注入到spring容器中

MapStruct类型转换接口未自动注入到spring容器中

MapStruct 是一个属性映射的注解处理器,适用于复杂系统(如分布式、微服务架构)的 POJO 类数据转换,尤其适合处理嵌套对象、集合映射等场景。与BeanUtils采用反射方式复制对象属性的方式不同,MapSturct通过编译时生成类型安全的映射代码,减少手动编写转换逻辑的需求,效率更优。

依赖

推荐使用1.5+版本

xml 复制代码
<dependency>
    <groupId>org.mapstruct</groupId>
    <artifactId>mapstruct</artifactId>
    <version>1.5.3.Final</version>
</dependency>

assembler接口

这里只需要定义一个assembler 对象转换接口,将不同的属性显式声明出来。这里注意接口上的注解@Mapper添加了componentModel = "spring"属性,这是自动注入为spring容器bean的前提。

java 复制代码
@Mapper(componentModel = "spring")
public interface UserLoginAssembler {

    @Mappings({
            @Mapping(source = "userName", target = "accountNumber"),
            @Mapping(source = "userPwd", target = "password"),
            @Mapping(source = "loginType", target = "loginType", defaultValue = "USER_LOGIN_ACCOUNT")
    })
    UserLoginCommand loginRequestConvert(UserLoginAdapterRepDTO userLoginAdapterRepDTO);
}

正常情况下,项目编译后,在assembler接口的同目录下会自动生成实现类文件,并且类标注了@Component注解。

启动报错

然而,事与愿违,服务启动时报错找不到assembler容器bean。

shell 复制代码
Parameter 6 of constructor in xxx.xxxService required a bean of type 'xxx.WorkflowAssembler' that could not be found.

折腾了一番,检查了依赖版本和lombok是否冲突,依赖注入等可能问题,均为有效处理。

compiler插件

前面提到,MapStruct实现原理是在编译时动态生成类型转换的实现方法,这里还需要在对应module的pom文件中显式声明maven-compiler-plugin的annotationProcessorPaths属性。

xml 复制代码
<build>
    <plugins>
	       <plugin>
	            <groupId>org.apache.maven.plugins</groupId>
	            <artifactId>maven-compiler-plugin</artifactId>
	            <configuration>
	                <source>1.8</source>
	                <target>1.8</target>
	                <annotationProcessorPaths>
	                    <path>
	                        <groupId>org.projectlombok</groupId>
	                        <artifactId>lombok</artifactId>
	                        <version>${lombok.version}</version>
	                    </path>
	                    <path>
	                        <groupId>org.mapstruct</groupId>
	                        <artifactId>mapstruct-processor</artifactId>
	                        <version>${mapstruct.version}</version>
	                    </path>
	                </annotationProcessorPaths>
	            </configuration>
	        </plugin>
      </plugins>
    </build>

最后还有提醒一下,如果你的项目是多个module,在主启动模块的pom中声明annotationProcessorPaths并不能贯通到所有依赖的模块中。比如在DDD的application层我们使用了MapStruct接口,就需要在这个module的pom文件中添加这个配置,仅在interface层添加后编译,实测无效。重点都可以在module编译后检查assembler接口是否自动编译生成了实现类。

相关推荐
星乐a1 分钟前
String 不可变性与常量池深度解析
java·开发语言
captain3761 分钟前
ACM模式下Java输入输出函数为什么会超时?及解决方法
java·开发语言
神の愛3 分钟前
mybatis什么时候不走 缓存??
spring·缓存·mybatis
程序员老邢7 分钟前
【产品底稿 04】商助慧 V1.1 里程碑:爬虫入库 + MySQL + Milvus 全链路打通
java·爬虫·mysql·ai·springboot·milvus
2601_950703949 分钟前
Java安全编程与静态分析实战
java
好家伙VCC10 分钟前
**发散创新:基于Python与OpenCV的视频流帧级分析实战**在当前人工智能与计算机视觉飞速发展的背景下
java·人工智能·python·计算机视觉
SimonKing11 分钟前
大V说’AI替代不了你’,但现实是——用AI的人正在替代你
java·后端·程序员
tryxr21 分钟前
SpringMVC 中的常用注解和用法
spring·mvc·springmvc
一叶龙洲26 分钟前
Java中使用模板引擎(FreeMarker / Velocity) + Word XML导出复杂Word
xml·java·word
Halo_tjn33 分钟前
Java 接口的定义重构学生管理系统
java·开发语言·算法