MapStruct不能使用Kodle和Spring Boot自动装配,而是使用Gradle构建的。

问题描述 投票:1回答:1

我的gradle文件中有以下部分:

apply plugin: 'kotlin-kapt'
...
    compile("org.mapstruct:mapstruct:1.3.0.Final")
    kapt("org.mapstruct:mapstruct-processor:1.3.0.Final")

而且还在使用JUnit 5。

我的映射器看起来像:

@Mapper(componentModel = "spring")
interface ModelMapper {
    fun convertToDto(forms: Model): DTO

    @InheritInverseConfiguration
    fun convertToModel(dto: DTO): Model
}

而且我正在尝试将其类似地自动接线:

@Service
@Transactional
class MyService @Autowired constructor(
    private val repository: MyRepository,
    private val mapper: ModelMapper
) {
...
}

但是当我尝试运行测试/进行构建时,出现错误:

...
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type '....ModelMapper' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}

有人知道为什么Spring Boot无法使用这种设置吗?

spring-boot gradle kotlin dependency-injection mapstruct
1个回答
0
投票

尝试添加

kapt(“ org.mapstruct:mapstruct-jdk8:1.3.1.Final”)

我正在使用kotlin和gradle在springboot中使用mapstruct。自动接线可以正常工作,这就是我所拥有的全部:

apply plugin: "kotlin-kapt"

compile "org.mapstruct:mapstruct:1.3.1.Final"

kapt("org.mapstruct:mapstruct-processor:1.3.1.Final")
kapt("org.mapstruct:mapstruct-jdk8:1.3.1.Final")

接口的配置:

@@ Mapper(componentModel =“ spring”,injectionStrategy =InjectionStrategy.CONSTRUCTOR)

© www.soinside.com 2019 - 2024. All rights reserved.