[英]how to use spring annotations like @Autowired or @Value in kotlin for primitive types?
Autowiring a non-primitive with spring annotations like使用 spring 注释自动装配非原始类型,例如
@Autowired
lateinit var metaDataService: MetaDataService
works.作品。
But this doesn't work:但这不起作用:
@Value("\${cacheTimeSeconds}")
lateinit var cacheTimeSeconds: Int
with an error:有错误:
lateinit modifier is not allowed for primitive types.
原始类型不允许使用 lateinit 修饰符。
How to autowire primitve properties into kotlin classes?如何将原始属性自动装配到 kotlin 类中?
You can also use the @Value annotation within the constructor:您还可以在构造函数中使用 @Value 注释:
class Test(
@Value("\${my.value}")
private val myValue: Long
) {
//...
}
This has the benefit that your variable is final and none-nullable.这样做的好处是您的变量是 final 且不可为空的。 I also prefer constructor injection.
我也更喜欢构造函数注入。 It can make testing easier.
它可以使测试更容易。
@Value("\\${cacheTimeSeconds}") lateinit var cacheTimeSeconds: Int
@Value("\\${cacheTimeSeconds}") lateinit var cacheTimeSeconds: Int
should be应该
@Value("\${cacheTimeSeconds}")
val cacheTimeSeconds: Int? = null
I just used Number
instead of Int
like so...我只是像这样使用
Number
而不是Int
......
@Value("\${cacheTimeSeconds}")
lateinit var cacheTimeSeconds: Number
The other options are to do what others mentioned before...其他选择是做其他人之前提到的......
@Value("\${cacheTimeSeconds}")
var cacheTimeSeconds: Int? = null
Or you can simply provide a default value like...或者您可以简单地提供一个默认值,例如...
@Value("\${cacheTimeSeconds}")
var cacheTimeSeconds: Int = 1
In my case I had to get a property that was a Boolean
type which is primitive in Kotlin, so my code looks like this...在我的例子中,我必须得到一个
Boolean
类型的属性,它在 Kotlin 中是原始类型,所以我的代码看起来像这样......
@Value("\${myBoolProperty}")
var myBoolProperty: Boolean = false
Try to set a default value尝试设置默认值
@Value("\${a}")
val a: Int = 0
in application.properties在 application.properties 中
a=1
in the code在代码中
package com.example.demo
import org.springframework.beans.factory.annotation.Value
import org.springframework.boot.CommandLineRunner
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.stereotype.Component
@SpringBootApplication
class DemoApplication
fun main(args: Array<String>) {
runApplication<DemoApplication>(*args)
}
@Component
class Main : CommandLineRunner {
@Value("\${a}")
val a: Int = 0
override fun run(vararg args: String) {
println(a)
}
}
it will print 1
它将打印
1
or use contructor inject或使用构造函数注入
@Component
class Main(@Value("\${a}") val a: Int) : CommandLineRunner {
override fun run(vararg args: String) {
println(a)
}
}
The problem is not the annotation, but the mix of primitive and lateinit
, as per this question , Kotlin does not allow lateinit
primitives.问题不在于注解,而是primitive 和
lateinit
的混合,根据这个问题,Kotlin 不允许lateinit
原语。
The fix would be to change to a nullable type Int?
解决方法是更改为可空类型
Int?
, or to not use lateinit
. ,或者不使用
lateinit
。
This TryItOnline shows the issue. 这个TryItOnline显示了这个问题。
Kotlin compiles Int to int in java code. Kotlin 在 Java 代码中将 Int 编译为 int。 Spring wanted non-primitive types for injection, so you should use Int?
Spring 需要非原始类型进行注入,所以你应该使用 Int? / Boolean?
/ 布尔值? / Long?
/ 长? and etc. Nullable types kotlin compile to Integer / Boolean / etc.
等。可空类型 kotlin 编译为 Integer / Boolean / 等。
From:从:
@Value("\${cacheTimeSeconds}") lateinit var cacheTimeSeconds: Int
To:到:
@delegate:Value("\${cacheTimeSeconds}") var cacheTimeSeconds by Delegates.notNull<Int>()
Good Luck祝你好运
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.