MapStruct中的抽象装饰器在其他映射结果中使用会导致模糊的异常。

huangapple go评论64阅读模式
英文:

Mapstructs abstract decorator used in other mapping results in ambiguous exception

问题

我实现了一个装饰器来自定义实体的映射,我们称之为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是否支持?

英文:

I implemented a decorator to customize the mapping of an entity, let's say MappingDecoratorA, which is an abstract class and implements the MapperA interface of mapstructs.

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

In an other mapping I use the MapperA, let's say MappingB, which uses cdi

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

Mapstructs generates two implementations for the MapperA, MapperAImpl and MapperAImpl_. In my situation the inject mechanism doesn't know which implementation to use. The result is an ambiguous exception listening the two implementation.

Does mapstruct support a solution for my problem?

答案1

得分: 2

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

因此,您问题的一个解决方案是执行以下操作:

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

When using the non default componentModel you have to use it for all the mappers. Especially if you want to reuse them. Otherwise the specific component won't know how to inject and create the mappers.

So a solution for your problem would be to do

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

huangapple
  • 本文由 发表于 2020年3月15日 19:28:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/60692376.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定