Spring Boot 支持data用于@ConfigurationProperties.
@ConstructorBinding
@ConfigurationProperties(prefix = "prefix")
data class AppProperties (
val something: String
)
Run Code Online (Sandbox Code Playgroud)
但是,val和@ConstructorBinding具有一定的局限性。您不能将一个变量别名为另一个变量。假设您在 Kubernetes 中运行并想要捕获由 env var 给出的主机名HOSTNAME。执行此操作的最简单方法是应用于@Value("\${HOSTNAME}:)"属性,但它仅适用于可变属性且没有构造函数绑定。
Spring Boot GitHub 问题跟踪器说:
停止!!请使用 spring-boot 标签在 Stack Overflow 上询问有关如何使用某些东西的问题,或者了解为什么某些东西没有按预期工作。
那么,这是一个已知的限制还是我应该为他们创建一张票来修复它?
编辑:打开https://github.com/spring-projects/spring-boot/issues/25552
I have below POJO in java which is used in Spring boot app to inject properties from YML during the app startup. Trying to convert the app into Kotlin but I have struggle implementing the values injected when I converted the POJO to data class.
@Component
@ConfigurationProperties("rest")
@Data
public class RestProperties {
private final Client client = new Client();
@Data
public static class Client {
private int defaultMaxTotalConnections;
private int defaultMaxConnectionsPerRoute;
private int defaultReadTimeout;
}
}
Run Code Online (Sandbox Code Playgroud)
I have tried below …