小编Kri*_*hna的帖子

使用 componentModel = "spring" 的 Mapstruct 依赖注入给出 null 对象

我正在尝试使用 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);
}
Run Code Online (Sandbox Code Playgroud)

TypeMapper的代码如下,

@Component
@Mapper
public abstract class TypeMapper {

  public abstract Type mapType(DtoType DtoType);

  @InheritConfiguration(name = "mapType")
  public abstract DtoType mapDtoType(Type type);
}
Run Code Online (Sandbox Code Playgroud)

生成的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())); …
Run Code Online (Sandbox Code Playgroud)

spring dependency-injection spring-boot mapstruct

7
推荐指数
1
解决办法
2万
查看次数

在JavaScript中让vs var

我知道let具有块作用域,而var具有功能作用域。但是我不明白在这种情况下,如何使用let解决问题

const arr = [1,2,3,4];
for (var i = 0; i < arr.length; i++) {
setTimeout(function() {
   console.log(arr[i]) 
}, 1000);
} // Prints undefined 5 times

const arr = [1,2,3,4];
for (let i = 0; i < arr.length; i++) {
setTimeout(function() {
   console.log(arr[i]) 
}, 1000);
} // Prints all the values correctly
Run Code Online (Sandbox Code Playgroud)

javascript closures var let

5
推荐指数
1
解决办法
864
查看次数