Mapstruct:如何自动装配抽象类映射器



我的问题似乎很容易,但可能是我做错了什么。这是一个带有@Named("toLocationListCommon")bean的映射器类,它应该在其他映射器中使用

@Mapper(componentModel = "spring")
public abstract class CommonLocationMapper {
@Setter(onMethod_ = @Autowired)
protected TestService testService;
@Named("toLocationListCommon")
List<Location> toLocationListCommon(List<? extends ILocation> loc) {
//do mapping logic
}
}

在这里我试着使用它:

@Mapper(implementationName = "V1Impl", config = CommonMapper.CommonMapperConfig.class, uses = CommonLocationMapper.class)
interface TestMapper {
//other mapping methods
}

我期望在TestMapper的实现中自动连接beanCommonLocationMapper,但我没有。我做错了什么?提前感谢!

@Mapper注释中的uses子句允许您的映射器在需要时使用您定义的映射器。如果你只想获取mapper的引用,你可以使用。

MyMapper INSTANCE = Mappers.getMapper( MyMapper.class );

最新更新