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接口是否自动编译生成了实现类。

相关推荐
圆山猫1 小时前
[Virtualization](四):Linux KVM/RISC-V 的 vCPU 运行路径
java·linux·risc-v
城管不管2 小时前
ReAct、Plan-and-Execute、Reflection 三大智能 Agent 范式核心区别
java·人工智能·算法·spring·ai·动态规划
IT小白杨2 小时前
从环境制备到自动化工作流:多账号运营的工程化架构拆解
java·经验分享·自动化·安全架构·指纹浏览器
豆瓣鸡3 小时前
算法日记 - Day3
java·开发语言·算法
萧瑟余晖3 小时前
Java深入解析篇九之NIO详解
java·网络·nio
The Chosen One9853 小时前
高进度算法模板速记(待完善)
java·前端·算法
极光代码工作室5 小时前
基于SpringBoot的课程预约系统
java·springboot·web开发·后端开发
Leighteen6 小时前
`try-finally` 里的 `return`:为什么 `finally` 会悄悄改掉返回值、吞掉异常
java·开发语言
名字还没想好☜7 小时前
Go 的 time.After 在 select 循环里内存泄漏:定时器堆积原理与 timer.Reset 正确姿势
java·数据库·golang·go·goroutine
圆山猫7 小时前
[Virtualization](三):RISC-V H-extension 与 Guest 执行模式
android·java·risc-v