Spring boot 科特林弹簧靴。控制器类@Validated cause@Autowired of service失败

Spring boot 科特林弹簧靴。控制器类@Validated cause@Autowired of service失败,spring-boot,validation,kotlin,service,autowired,Spring Boot,Validation,Kotlin,Service,Autowired,我在HomeController上使用@Validated,然后用户服务的@Autowired失败。下面我有个例外 kotlin.UninitializedPropertyAccessException: lateinit property service has not been initialized at com.example.demo.web.HomeController.getService(HomeController.kt:16) ~[main/:na] at c

我在
HomeController
上使用
@Validated
,然后用户服务的
@Autowired
失败。下面我有个例外

kotlin.UninitializedPropertyAccessException: lateinit property service has not been initialized
    at com.example.demo.web.HomeController.getService(HomeController.kt:16) ~[main/:na]
    at com.example.demo.web.HomeController.index(HomeController.kt:20) ~[main/:na]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke...
  • 弹簧靴
    2.4.5
  • Kotlin
    1.5.0
  • 渐变
    6.8.3-bin
这是我的密码

HoneController.kt

@已验证
@RestController
开放式家庭控制器{
@自动连线
lateinit var服务:UserSercie
@请求映射(“/”)
趣味索引(@NotNull(message=“name不能为null”)名称:String:String{
返回“hello”+name+service.getData()
}
}
UserService.kt

@服务
类UserSercie{
fun getData():字符串{
返回“来自服务的消息”
}
}
build.gralde

plugins {
    id 'org.springframework.boot' version '2.4.5'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    id 'java'
    id 'org.jetbrains.kotlin.jvm' version '1.5.0'
}

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-validation'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    compileOnly 'org.projectlombok:lombok'
    annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
    annotationProcessor 'org.projectlombok:lombok'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
}

test {
    useJUnitPlatform()
}
compileKotlin {
    kotlinOptions {
        jvmTarget = "11"
    }
}
compileTestKotlin {
    kotlinOptions {
        jvmTarget = "11"
    }
}

我找到了kotlin spring插件:https://kotlinlang.org/docs/all-open-plugin.html#spring-支持

我使用插件并在
HomeController
中删除
open
。 然后它就起作用了

buildscript {

    repositories {
        mavenCentral()
    }

    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:$springbootVersion")
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath "org.jetbrains.kotlin:kotlin-allopen:$kotlin_version"
    }
}

subprojects {

    apply plugin: 'java'
    apply plugin: 'war'
    apply plugin: 'kotlin'
    apply plugin: "kotlin-spring"
    apply plugin: 'org.springframework.boot'
    apply plugin: 'io.spring.dependency-management'

    //other settings
}