Mapstruct结合Spring条件注解实现唯一EntityMapper Bean实例
一、核心问题分析
你当前的配置中,@ConditionalOnProperty注解是加在Mapper接口上的,但MapStruct生成的实现类(如EntityAMapperImpl)默认不会继承这个注解——因为Spring的条件注解是作用在Bean类上的,而MapStruct只会自动给实现类添加@Component注解(由componentModel = SPRING配置),不会传递接口上的非MapStruct注解。这会导致所有Mapper实现类都被Spring注册,进而出现多个EntityMapper类型Bean的冲突。
二、让MapStruct自动为实现类添加@ConditionalOnProperty
有两种可行方式实现这一需求:
方法1:直接通过@Mapper的annotations属性指定
在每个子Mapper接口的@Mapper注解中,使用annotations参数直接声明要添加到实现类的条件注解。示例如下:
@Mapper( config = EntityMapperConfig.class, annotations = @ConditionalOnProperty(value = "propKey", havingValue = "entityA") ) public interface EntityAMapper extends EntityMapper { // 映射方法定义 }
MapStruct在生成EntityAMapperImpl时,会自动为该类加上指定的@ConditionalOnProperty注解。这种方式简单直接,适合子接口数量不多的场景。
方法2:自定义MapStruct扩展(进阶)
如果子接口数量较多,不想重复编写注解,可以自定义MapStruct的注解处理器扩展,实现自动将接口上的@ConditionalOnProperty复制到生成的实现类中。不过这种方法需要编写自定义Processor,复杂度较高,适合大规模项目场景。
三、确保Spring上下文仅存在一个EntityMapper实现Bean
通过上述方法为每个生成的Impl类添加对应条件注解后,Spring会根据propKey的取值,仅实例化符合条件的那个实现类:
- 当配置文件中
propKey=entityA时,只有EntityAMapperImpl会被注册; - 当
propKey=entityB时,只有EntityBMapperImpl会被注册; - 若需要默认实现,可以给其中一个子接口的注解添加
matchIfMissing = true,比如@ConditionalOnProperty(value = "propKey", havingValue = "entityA", matchIfMissing = true),这样未配置propKey时会默认实例化该实现类。
版本要求
注意:通过@Mapper.annotations传递注解的功能需要MapStruct 1.4.0及以上版本支持,请确保项目依赖的MapStruct版本符合要求。
内容的提问来源于stack exchange,提问作者peppo

