spring-boot - Kotlin 弹簧靴。控制器类@Validated 导致@Autowired 的服务失败
问题描述
我使用@Validated
on HomeController
,然后@Autowired
UserService 失败。我在下面得到异常
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
- 科特林
1.5.0
- 摇篮
6.8.3-bin
这是我的代码
HoneController.kt
@Validated
@RestController
open class HomeController {
@Autowired
lateinit var service: UserSercie
@RequestMapping("/")
fun index(@NotNull(message = "name can not be null") name: String): String {
return "hello " + name + service.getData()
}
}
用户服务.kt
@Service
class UserSercie {
fun getData(): String {
return " message from service"
}
}
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 plugin
了:https://kotlinlang.org/docs/all-open-plugin.html#spring-support
我使用插件并open
在HomeController
. 然后它工作。
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
}
推荐阅读
- vuejs2 - 将计算数据作为道具传递给子组件的模式(Promise 问题)
- mysql - 错误 pool.db:错误:ER_CON_COUNT_ERROR:使用 MySql 的连接太多
- javascript - can't get random # 通过 switch 语句
- java - 如何跨返回整数的不同类访问共享bean实例
- java - 使用 Java 进行自适应卡片渲染和创作
- java - 错误:运行 Spring Boot 应用程序时,由于缺少 EmbeddedServletContainerFactory bean,无法启动 EmbeddedWebApplicationContext
- python - 如何在 tkinter 中修复 scrolledtext 小部件的滚动条的位置?
- video - 即使在转码后,FFMPEG音频/视频在剪切和连接后也不同步
- java - 将功能移动到另一个活动后,无法从 Firestore 中检索文档
- excel - 带有 NamedRange 的 VLookup 函数被引用为字符串