MapStruct不使用Kotlin和Sping Boot 自动装配,使用Gradle构建

vm0i2vca  于 2023-08-06  发布在  Kotlin
关注(0)|答案(5)|浏览(132)

我的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看起来像:

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


有谁知道为什么Sping Boot 不支持这种设置吗?

z9zf31ra

z9zf31ra1#

尝试将Spring的annotation processor传递给build.gradle中的kapt:

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

字符串
或者尝试保留javac注解处理器:

kapt {
    keepJavacAnnotationProcessors = true
}

oprakyz7

oprakyz72#

这帮助了我,在你的构建中添加这个。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")

字符串

k4emjkb1

k4emjkb13#

这就是solution
只是pom.xml必须包含一些更多的信息:

<executions>
 <execution>
     <id>compile</id>
     <phase>process-sources</phase>
     <goals>
         <goal>compile</goal>
     </goals>
 </execution>
 <execution>
     <id>test-compile</id>
     <phase>test-compile</phase>
     <goals>
         <goal>test-compile</goal>
     </goals>
 </execution>
 <execution>
     <id>kapt</id>
     <goals>
         <goal>kapt</goal>
     </goals>
     <configuration>
         <sourceDirs>
             <sourceDir>src/main/kotlin</sourceDir>
             <sourceDir>src/main/java</sourceDir>
         </sourceDirs>
         <annotationProcessorPaths>
             <annotationProcessorPath>
                 <groupId>org.mapstruct</groupId>
                 <artifactId>mapstruct-processor</artifactId>
                 <version>${mapstruct.version}</version>
             </annotationProcessorPath>
         </annotationProcessorPaths>
     </configuration>
 </execution>

字符串
然后就成功了

hlswsv35

hlswsv354#

如果你有一个@Transactional class{},那么你必须让它的所有方法打开open fun(){}来启用bean Autowiring
kotlin spring boot plugin改变了Kotlin的默认行为,它默认打开所有类和方法
只需在项目的build.gradle中添加Kotlinspring Boot 插件
groovy:

plugins {
    ....
    id 'org.jetbrains.kotlin.plugin.spring' version '1.8.22'
}

字符串
Kotlin

plugins {
    ....
    kotlin("plugin.spring") version "1.8.22"
}

7kqas0il

7kqas0il5#

尝试添加
kapt(“org.mapstruct:mapstruct-jdk8:1.3.1.Final”)
我在springboot中使用mapstruct,使用Kotlin和gradle。和一切工作正常与autowiring,这就是我所拥有的:

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)

相关问题