Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
929 views
in Technique[技术] by (71.8m points)

spring-boot - 使用Gradle构建的Kotlin和Spring Boot无法自动装配MapStruct(MapStruct not autowiring with Kotlin and Spring Boot, built using Gradle)

I have the following parts in my gradle file:

(我的gradle文件中包含以下部分:)

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

And am also using JUnit 5.

(而且我还在使用JUnit 5。)

My mapper looks like:

(我的映射器看起来像:)

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

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

And I'm trying to autowire it similar to such:

(我正在尝试将其类似于以下内容进行自动布线:)

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

But when I try to run a test/do a build, I get an error:

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

...
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: {}

Does anyone know why Spring Boot isn't working with this kind of setup?

(有谁知道为什么Spring Boot无法使用这种设置?)

  ask by ben l translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try to add

(尝试添加)

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

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

I'm using mapstruct in springboot using kotlin and gradle.

(我在Springboot中使用kotlin和gradle使用mapstruct。)

And everything works fine with autowiring, that's all what i have:

(自动装配一切正常,这就是我所拥有的:)

 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") 

And configuration for the interface:

(并配置接口:)

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...