英文:
@ConfigurationProperties without default values in spring boot 3 using Kotlin
问题
我尝试使用Kotlin实现我的第一个Spring Boot应用程序。
spring-boot的版本是3.1.0。
我有一个配置类,像这样(基于https://stackoverflow.com/a/74445643/2674303):
@Configuration
@ConfigurationProperties(prefix = "my.prefix")
data class MyProperties(
var username: String = "",
var privateKeyPath: String = "",
这个工作正常,但我不想在这里看到默认值,因为它们是无用的,在Java中你不必要有它们。
我找到了以下帖子:
https://stackoverflow.com/questions/45953118/kotlin-spring-boot-configurationproperties
并开始应用那里的解决方案:
1.
import org.springframework.boot.context.properties.bind.ConstructorBinding
@Configuration
@ConfigurationProperties(prefix = "my.prefix")
@ConstructorBinding
data class MyProperties(
var username: String = "",
var privateKeyPath: String = "",
我得到了编译错误:
此注释不适用于目标'class'。
2.
@Configuration
@ConfigurationProperties(prefix = "my.prefix")
data class MyProperties {
lateinit var username: String = "",
lateinit var privateKeyPath: String = "",
我收到一个指向第一个字段的错误:
需要属性getter或setter
我有什么遗漏吗?
英文:
I try to implement my first spring boot application using Kotlin.
spring-boot version is 3.1.0
I have a configuration class like this(based on https://stackoverflow.com/a/74445643/2674303):
@Configuration
@ConfigurationProperties(prefix = "my.prefix")
data class MyProperties(
var username: String = "",
var privateKeyPath: String = "",
This works fine but I don't want to see default values here because they are useless and in java you don't have to have them.
I've found following post:
https://stackoverflow.com/questions/45953118/kotlin-spring-boot-configurationproperties
And started to apply solutions from there:
1.
import org.springframework.boot.context.properties.bind.ConstructorBinding
@Configuration
@ConfigurationProperties(prefix = "my.prefix")
@ConstructorBinding
data class MyProperties(
var username: String = "",
var privateKeyPath: String = "",
I get compilation error:
This annotation is not applicable to target 'class'
2.
@Configuration
@ConfigurationProperties(prefix = "my.prefix")
data class MyProperties {
lateinit varusername: String = "",
lateinit var privateKeyPath: String = "",
I receive an error which points to the first fileld:
Property getter or setter expected
Have I missed something ?
答案1
得分: 0
这段代码有效:
@ConfigurationProperties(prefix = "my.prefix")
数据类 MyProperties {
lateinit varusername: String = "",
lateinit var privateKeyPath: String = "",
...
@Configuration
@EnableConfigurationProperties(MyProperties ::class)
类 LdapConnectionPoolConfig(
私有 val myProperties : MyProperties
) {
....
英文:
This works:
@ConfigurationProperties(prefix = "my.prefix")
data class MyProperties {
lateinit varusername: String = "",
lateinit var privateKeyPath: String = "",
...
@Configuration
@EnableConfigurationProperties(MyProperties ::class)
class LdapConnectionPoolConfig(
private val myProperties : MyProperties
) {
....
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论