我在使用抽象类的 MapStruct 方面遇到了一些问题。 我有 2 个映射器:
MapperA extends AbstractMapper<U,V>
MapperB extends AbstractMapper<U,V>
MapperA 使用 MapperB
我在AbstractMapper
中有一个方法
public <T extends AbstractReference> T resolveReference(String id, @TargetType Class<T> entityClass) {
// Some implementation
}
在全新安装时,我遇到了不明确的方法错误。
Mapstruct 似乎找到了该方法两次,每个 Mapper 都扩展了同一个类。
我对限定符进行了一些搜索,但在使用具有相同签名的不同方法时它似乎很有用。但就我而言,是同一个!!
如果你有任何整数。
谢谢
编辑:
@Mapper(componentModel = "cdi", uses = {MapperB.class})
@ApplicationScoped
public abstract class MapperA extends AbstractMapper<U1,V1> {}
MapperB 不使用任何其他映射器。
@Mapper(componentModel = "cdi")
@ApplicationScoped
public abstract class MapperB extends AbstractMapper<U2,V2> {}
最佳答案
你实际上没有一种方法,你有两种方法。一个在 MapperA
中,另一个在 MapperB
中。 MapStruct 不关心继承关系和每个方法所在的位置。
还有你的方法
public <T extends AbstractReference> T resolveReference(String id, @TargetType Class<T> entityClass) {
// Some implementation
}
是一个泛型,具有相同的删除,并且不依赖于AbstractMapper
泛型参数,导致MapStruct出现二义性方法错误。
我不确定您在该方法中实际上在做什么。但如果它是不依赖于 AbstractMapper
泛型参数 U
和 V
的通用方法,那么我建议您将此方法提取到一个不同的类并将该类添加到 uses
注释变量中。
如果方法依赖于参数,那么请确保它们是其中的一部分,然后 MapStruct 将正常工作,因为它们在编译期间将具有不同的类型(U1、V1
或 U2,V2
)
关于java - MapStruct:抽象类的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41978330/