'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?
Solution 1:[1]
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
}
Solution 2:[2]
Try to add
kapt("org.mapstruct:mapstruct-jdk8:1.3.1.Final")
I'm using mapstruct in springboot using kotlin and gradle. 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)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Georgian |
Solution 2 | Andrew Zweiseite |