【发布时间】:2019-06-17 15:38:07
【问题描述】:
我正在尝试使用 Spring 注入映射器对象(类为 TypeMapper)依赖项,如下所示,
@Mapper(componentModel = "spring",
uses = {TypeMapper.class})
public interface AttachmentMapper {
AttachmentMapper MAPPER = Mappers.getMapper(AttachmentMapper.class);
@Mappings({
@Mapping(source = "type", target = "type") })
AttachmentDTO toDTO(Attachment attachment);
}
TypeMapper的代码如下,
@Component
@Mapper
public abstract class TypeMapper {
public abstract Type mapType(DtoType DtoType);
@InheritConfiguration(name = "mapType")
public abstract DtoType mapDtoType(Type type);
}
生成的AttachmentMapperImpl代码如下,
public class AttachmentMapperImpl implements AttachmentMapper {
@Autowired
private TypeMapper typeMapper;
public AttachmentDto toDTO(Attachment attachment) {
if ( attachment == null) {
return null;
}
attachmentDTO.setType(typeMapper.mapDtoType(attachment.getType()));
return attachmentDTO;
}
问题出在生成的代码中,@Autowired typeMapper 为空。谁能阐明我在这里做错了什么?
【问题讨论】:
标签: spring spring-boot dependency-injection mapstruct