Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MapStruct not autowiring with Kotlin and Spring Boot, built using Gradle

I have the following parts in my gradle file:

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.

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?

like image 681
ben l Avatar asked Sep 07 '25 10:09

ben l


2 Answers

Try passing Spring's annotation processor to kapt in build.gradle:

kapt("org.springframework.boot:spring-boot-configuration-processor:${springBootVersion}")

or try keeping javac annotation processors:

kapt {
    keepJavacAnnotationProcessors = true
}
like image 121
Georgian Avatar answered Sep 09 '25 22:09

Georgian


this helped me, add this in your build.gradle.kts:

    implementation("org.mapstruct:mapstruct")
    kapt("org.mapstruct:mapstruct-processor")

    compileOnly(group = "org.springframework.boot", name = "spring-boot-configuration-processor")
    kapt(group = "org.springframework.boot", name = "spring-boot-configuration-processor")
like image 30
meiskalt7 Avatar answered Sep 09 '25 22:09

meiskalt7