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

时间:2017-12-25 04:44:48

标签: java spring mapstruct

我有一个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。

但是IntEnumConverter和DownloadModelHelper是正常的类。

启动弹簧启动时出现以下错误:

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组件和普通类?

1 个答案:

答案 0 :(得分:0)

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