简体   繁体   English

使用的 Mapstruct 不会实例化 class

[英]Mapstruct with uses does not instantiate class

I have the following Mapper:我有以下映射器:

@Mapper(componentModel="spring", uses = {DrugstoreService.class})
public abstract class PreregisteredPharmacistMapper {

    @Mapping(source = "drugstoreId", target = "drugstore")
    public abstract PreregisteredPharmacist toEntity (
            PreregisteredPharmacistDTO preregisteredPharmacistDTO
    );

    public abstract void toEntityUpdate (
            @MappingTarget PreregisteredPharmacist preregisteredPharmacist,
            PreregisteredPharmacistDTO preregisteredPharmacistDTO
    );

    public abstract PreregisteredPharmacistDTO toDTO(
            PreregisteredPharmacist preregisteredPharmacist
    );
}

DrugstoreService is an interface with the following implementation: DrugstoreService 是一个具有以下实现的接口:

@Service
public class DrugstoreServiceImpl implements DrugstoreService {

    private DrugstoreRepository drugstoreRepository;

    /**
     * DrugstoreServiceImpl constructor.
     *
     * @param drugstoreRepository
     */
    @Autowired
    public DrugstoreServiceImpl (
            DrugstoreRepository drugstoreRepository
    ) {
        this.drugstoreRepository = drugstoreRepository;
    }

    @Override
    public Drugstore findEntityById(Integer id) {
        Optional<Drugstore> drugstore = drugstoreRepository.findById(id);
        if (!drugstore.isPresent()) {
            throw new ResourceNotFoundException("Drugstore", "id", id);
        }

        return drugstore.get();
    }
}

When trying to use the mapper, a NullPointerException is thrown because DrugstoreService is not instantiated in the mapper's implementation.尝试使用映射器时,会引发 NullPointerException,因为 DrugstoreService 未在映射器的实现中实例化。 Here is a screenshot from debugging the code:这是调试代码的屏幕截图: 在此处输入图像描述 The implementation for the mapper is generated.生成映射器的实现。 So why is drugstoreService null?那么为什么是drugstoreService null?

When using componentModel different then the default one you have to use the appropriate dependency injection framework to instantiate your mappers.当使用不同于默认的componentModel时,您必须使用适当的依赖注入框架来实例化您的映射器。 In your case you have to use Spring to get your mapper and not instantiate it manually.在您的情况下,您必须使用 Spring 来获取您的映射器,而不是手动实例化它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM