Hi this似乎适用于添加额外的方法,但不适用于在现有方法上添加新注释。假设我们有以下类:
public class SourceClass {
private String field_1;
private String field_2;
}
public class TargetParent {
private String field_a;
}
public class TargetChild extends TargetParent {
private String field_b;
}父对象是框架的一部分,可以由其他方扩展。子对象是添加新字段的具体扩展的一个示例。
我正在计划一种映射层次结构的方法,如下所示:
@Mapper
public interface ParentMapper {
@Mapping(source="field_1", target="field_a")
public TargetParent convert(SourceClass source);
}
@Mapper
public interface ChildMapper extends ParentMapper {
@Mapping(source="field_2", target="field_b")
public TargetChild convert(SourceClass source);
}我希望看到ChildMapper的实现具有以下特点:
public TargetChild convert(SourceClass source){
// target instanciation through factory here
target.setField_a(field_1);
target.setField_b(field_2);
}但这不起作用:@Mapping注解似乎不会被继承。
我可以试着找出一些替代的解决方案,但似乎我开始破解框架,这不是我的意图。
我是不是漏掉了什么?
发布于 2021-05-15 16:36:08
如果你想继承某些注解,你应该使用Mapping configuration inheritance。
例如:
@Mapper
public interface ParentMapper {
@Mapping(source="field_1", target="field_a")
public TargetParent convert(SourceClass source);
}
@Mapper(config = ParentMapper.class)
public interface ChildMapper {
@Mapping(source="field_2", target="field_b")
@InheritConfiguration
public TargetChild convert(SourceClass source);
}https://stackoverflow.com/questions/67508950
复制相似问题