【问题标题】:How resolve error injecting bean MapStruct in Spring如何解决 Spring 中注入 bean MapStruct 的错误
【发布时间】:2022-07-17 20:36:27
【问题描述】:

我正在尝试使用 mapstruct 注入我的映射器,但 spring 无法识别 bean。

这是我的映射器

package com.api.gestioncartera.Services.Mappers;

import org.mapstruct.Mapper;
import org.springframework.stereotype.Component;

import com.api.gestioncartera.Entities.CollectionCompany;
import com.api.gestioncartera.Services.DTO.CollectionCompanyDto;

@Mapper(componentModel = "spring")
public interface CollectionCompanyMapper {
    CollectionCompanyDto collectionCompanyToCollectionCompanyDto(CollectionCompany collectionCompany);
}

我正在尝试注入我的服务

@Service
@Transactional
public class CollectionCompanyServiceImp implements CollectionCompanyService{
    
    @Autowired
    private CollectionCompanyMapper companyMapper;
}

我的 gradle 配置

plugins {
    id 'org.springframework.boot' version '2.5.6'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    id 'java'
}
...
dependencies {
    ...
    implementation 'org.mapstruct:mapstruct:1.4.2.Final'
 
    annotationProcessor 'org.mapstruct:mapstruct-processor:1.4.2.Final'

}
compileJava {
    options.compilerArgs += [
        '-Amapstruct.suppressGeneratorTimestamp=true',
        '-Amapstruct.suppressGeneratorVersionInfoComment=true',
        '-Amapstruct.verbose=true',
        '-Amapstruct.defaultComponentModel=spring'
    ]
}

我还在 IDE 中启用了启用注释处理 Properties in the IDE

错误是:

考虑在您的配置中定义一个“com.api.gestioncartera.Services.Mappers.CollectionCompanyMapper”类型的 bean。

我注意到我没有任何插件引用 mapstruct,这可能是问题吗?图片:

我正在使用 Spring Tool Suite 4 (Eclipse) + Gradle 6.8 + SrpingBoot 2.5.6 请帮忙!!

【问题讨论】:

  • 您是否检查了 CollectionCompanyMapper 的实现是否正确生成,在项目中编译并查看
  • 如何查看?
  • 检查 target/generated_sources 文件夹
  • 我运行项目并注释了 inejction,我检查了文件夹 .apt_generated 并且是空的,MapStruct 没有创建实现
  • 我没有“target/generated_sources”文件夹

标签: spring spring-boot mapstruct mapper


【解决方案1】:

Eclipse 在注释处理方面存在问题。

我使用这个插件解决了我的项目的问题:

https://plugins.gradle.org/plugin/

将此添加到 gradle 插件的顶部。

plugins {
  id "eclipse"
  id "com.diffplug.eclipse.apt" version "3.37.1"
}

然后进行 gradle 刷新。

如果还是不行,运行

./gradlew eclipse eclipseJdtApt eclipseFactorypath

希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 2019-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-05
    • 1970-01-01
    • 2013-06-25
    相关资源
    最近更新 更多