【发布时间】:2021-06-16 04:37:11
【问题描述】:
Java 16 引入了Records,这有助于在编写携带不可变数据的类时减少样板代码。当我尝试将 Record 用作 @ConfigurationProperties bean 时,如下所示,我收到以下错误消息:
@ConfigurationProperties("demo")
public record MyConfigurationProperties(
String myProperty
) {
}
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of constructor in com.example.demo.MyConfigurationProperties required a bean of type 'java.lang.String' that could not be found.
如何将记录用作@ConfigurationProperties?
【问题讨论】:
-
当您还需要@Configuration 注释时,这些注释不能是最终的,但记录是,这应该如何工作?
-
@SebastiaanvandenBroek 您可以在 EnableConfigurationProperties 配置中定义它,而不是使用配置注释
标签: java spring spring-boot java-16