使用Mapstruct重用装饰的映射器

时间:2019-05-24 06:42:12

标签: java spring mapstruct

我正在使用MapStruct 1.3.0.Final将Dto转换为POJO,反之亦然,将 spring 值转换为 mapstruct.defaultComponentModel 选项。为了避免由于双向关系引起的堆栈溢出错误,我遵循了下一个链接:

Mapping With Cycles

其中一些转换器已按照以下说明进行装饰:

Customizing Mappers Using Decorators

问题是:

如何使用使用选项在其他转换器中使用修饰的转换器?

按照第二个链接中包含的示例,我已经测试:

@Mapper(componentModel="spring", uses={PersonMapper.class})
public interface OtherMapper

@Mapper(componentModel="spring", uses={PersonMapperDecorator.class})
public interface OtherMapper

但是我还没有意识到OtherMapper使用PersonMapperDecorator

1 个答案:

答案 0 :(得分:0)

将MapStruct装饰器与Spring组件模型一起使用时,您只需要使用映射器,而不需要装饰器。

在您的示例中,您应该使用

@Mapper(componentModel = "spring", uses = PersonMapper.class)
public interface OtherMapper {
    ...
}

这将注入PersonMapper装饰器以及注入的原始映射器(PersonMapper)。