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

相关推荐
echoyu.8 分钟前
消息队列-kafka完结
java·分布式·kafka
七夜zippoe12 分钟前
分布式事务性能优化:从故障现场到方案落地的实战手记(二)
java·分布式·性能优化
栀椩12 分钟前
springboot配置请求日志
java·spring boot·后端
番薯大佬27 分钟前
Python学习-day8 元组tuple
java·python·学习
何似在人间57527 分钟前
Go语言快速入门教程(JAVA转go)——1 概述
java·开发语言·golang
疯子@1231 小时前
nacos1.3.2 ARM 版容器镜像制作
java·linux·docker·容器
Swift社区1 小时前
如何解决 Spring Bean 循环依赖
java·后端·spring
我真的是大笨蛋1 小时前
从源码和设计模式深挖AQS(AbstractQueuedSynchronizer)
java·jvm·设计模式
Pretend° Ω1 小时前
LRU缓存详解:用C语言实现高效数据管理
运维·c语言·spring·缓存·lru·双向链表
空山新雨(大队长)2 小时前
Java第五课:输入输出
java·开发语言