如何在mapstruct中一起使用spring组件和普通类?

问题描述 投票:0回答:1

我有一个mapper应该使用spring组件类和普通类来转换字段。

这个映射器如下:

@Mapper(componentModel = "spring", uses = { ImagesConverter.class, DateTimeConvertor.class, IntEnumConverter.class,
    DownloadModelHelper.class }, nullValueMappingStrategy = NullValueMappingStrategy.RETURN_DEFAULT)
public interface PictureMapper {
PictureMapper TRANSLATOR = Mappers.getMapper(PictureMapper.class);

@Mappings({ @Mapping(source = "picture.img", target = "downloadModel"),
        @Mapping(source = "picture.id", target = "pictureId"),
        @Mapping(source = "galleryPicture.id", target = "galleryPictureID") })
PictureModel entityToModel(Picture entity);

@Mapping(source = "pictureId", target = "picture")
Picture modelToEntity(PictureModel model);

List<PictureModel> entitiesToModels(List<Picture> entityList);

List<Picture> modelsToEntities(List<PictureModel> modelList);

}

ImagesConverter和DateTimeConverter是使用@Component的spring组件类。

但IntEnumConverter和DownloadModelHelper是普通类。

当启动spring boot时出现以下错误:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'pictureMapperImpl': Unsatisfied dependency expressed through field 'downloadModelHelper': No qualifying bean of type [ir.pt.discountnetwork.commons.tools.DownloadModelHelper] found for dependency [ir.pt.discountnetwork.commons.tools.DownloadModelHelper]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations

如何在mapstruct中一起使用spring组件和普通类?

java spring mapstruct
1个回答
0
投票

也许,你做不到。 Spring初始化java类。您可以使用组件将其放入弹簧容器中,也可以手动初始化。

© www.soinside.com 2019 - 2024. All rights reserved.