我定义了一个mapstruct接口,它使用了另外两个mapstruct映射器:
@Mapper(componentModel = "spring", uses = {Mapper1.class, Mapper2.class})
public interface ParentMapper{
}
现在我想在 ParentMapper 中引用这两个映射器,即我希望能够访问具体实现
mapper1
和 mapper2
,因为我需要在我想要创建的默认方法中使用它们。我希望通过使用 @Autowired 注入映射器来实现这一点:
@Autowired
Mapper1 mapper1;
@Autowired
Mapper2 mapper2;
但是,这不起作用,因为映射器似乎没有初始化。由于映射器是由 Spring 注入的,我不明白为什么它们不会被初始化。有人可以帮我吗?
将您的ParentMapper接口设为抽象类,它将能够使用@Autowired功能。
@Mapper(componentModel = "spring", uses = {Mapper1.class, Mapper2.class})
public abstract class ParentMapper{
@Autowired
Mapper1 mapper1;
@Autowired
Mapper2 mapper2;
}
此后,您将可以使用mapper1和mapper2的默认功能。