【问题标题】:Mapstructs abstract decorator used in other mapping results in ambiguous exception其他映射中使用的 Mapstructs 抽象装饰器导致模棱两可的异常
【发布时间】:2020-03-15 22:25:02
【问题描述】:

我实现了一个装饰器来自定义一个实体的映射,比如说MappingDecoratorA,它是一个抽象类,实现了mapstructs的MapperA接口。

public abstract class MappingDecoratorA implements MapperA {
...}
@Mapper
@DecoratedWith(MappingDecoratorA .class)
public interface MapperA {

在另一个映射中,我使用 MapperA,比如说 MappingB,它使用 cdi

@Mapper(uses = { MapperA.class},
    componentModel = "cdi")
public interface MapperB{

Mapstructs 为 MapperA 生成两个实现,MapperAImpl 和 MapperAImpl_。在我的情况下,注入机制不知道要使用哪个实现。结果是一个模棱两可的异常,监听了这两个实现。

mapstruct 是否支持解决我的问题?

【问题讨论】:

    标签: java mapstruct


    【解决方案1】:

    当使用非默认componentModel 时,您必须将它用于所有映射器。特别是如果您想重用它们。否则特定组件将不知道如何注入和创建映射器。

    所以你的问题的解决方案是这样做

    @Mapper(componentModel = "cdi")
    @DecoratedWith(MappingDecoratorA .class)
    public interface MapperA {
    }
    

    【讨论】:

    • 感谢您的建议。这是一个很好的提示。最终的解决方案是使用 CDI 装饰器而不是 mapstruct 装饰器。使用带有 beans.xml 条目的 CDI 装饰器解决了这个问题。解决方案就在我面前:manual :-)
    猜你喜欢
    • 1970-01-01
    • 2015-05-19
    • 1970-01-01
    • 2023-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-04
    相关资源
    最近更新 更多