簡體   English   中英

使用的 Mapstruct 不會實例化 class

[英]Mapstruct with uses does not instantiate class

我有以下映射器:

@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 是一個具有以下實現的接口:

@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();
    }
}

嘗試使用映射器時,會引發 NullPointerException,因為 DrugstoreService 未在映射器的實現中實例化。 這是調試代碼的屏幕截圖: 在此處輸入圖像描述 生成映射器的實現。 那么為什么是drugstoreService null?

當使用不同於默認的componentModel時,您必須使用適當的依賴注入框架來實例化您的映射器。 在您的情況下,您必須使用 Spring 來獲取您的映射器,而不是手動實例化它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM