0
votes

I have a mapper be should use of spring component class and normal class for convert fields.

this mapper in following :

@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 and DateTimeConverter is spring component class be use of @Component.

but IntEnumConverter and DownloadModelHelper is normal class.

when start spring boot get following error:

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

how to use spring component and normal class with together in mapstruct?

1
I think DownloadModelHelper also need to be spring component or bean but is this like that ? - Mithat Konuk
DownloadModelHelper is normal class in another project.can't add @component annotation to this class - or123456
well you can use like this in your configuration as @bean downloadModelHelper.class - Mithat Konuk
When you use one of the dependency injection componentModel you can't mix the instantiation of the classes. Mapstruct will always inject the required classes. You can define a bean in a configuration class like Mithat suggested - Filip

1 Answers

0
votes

Perhapsly, you can't do that. Spring intializes java classes. Either you put it inside spring container using component or manually initialize.