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

相关推荐
徐徐同学3 小时前
cpolar为IT-Tools 解锁公网访问,远程开发再也不卡壳
java·开发语言·分布式
Mr.朱鹏4 小时前
Nginx路由转发案例实战
java·运维·spring boot·nginx·spring·intellij-idea·jetty
白露与泡影6 小时前
2026版Java架构师面试题及答案整理汇总
java·开发语言
历程里程碑6 小时前
滑动窗口---- 无重复字符的最长子串
java·数据结构·c++·python·算法·leetcode·django
qq_229058016 小时前
docker中检测进程的内存使用量
java·docker·容器
我真的是大笨蛋6 小时前
InnoDB行级锁解析
java·数据库·sql·mysql·性能优化·数据库开发
钦拆大仁7 小时前
Java设计模式-单例模式
java·单例模式·设计模式
小手cool7 小时前
在保持数组中对应元素(包括负数和正数)各自组内顺序不变的情况下,交换数组中对应的负数和正数元素
java
笨手笨脚の7 小时前
深入理解 Java 虚拟机-04 垃圾收集器
java·jvm·垃圾收集器·垃圾回收
skywalker_117 小时前
Java中异常
java·开发语言·异常