如何将@ConfigurationProperties 与记录一起使用?
How to use @ConfigurationProperties with Records?
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
?
回答我自己的问题。
上述错误源于 Spring 由于缺少无参数构造函数,Boot 无法构造 bean。记录为每个成员隐式声明一个带有参数的构造函数。
Spring Boot 允许我们使用 @ConstructorBinding
注释来启用 属性 构造函数绑定而不是 setter 方法(如 the docs and the answer to this question 中所述) .这也适用于记录,所以这有效:
@ConfigurationProperties("demo")
@ConstructorBinding
public record MyConfigurationProperties(
String myProperty
) {
}
更新:从 Spring Boot 2.6 开始,使用记录开箱即用,当记录只有一个构造函数时,不再需要 @ConstructorBinding
。见 release notes.
如果您需要以编程方式声明默认值:
@ConfigurationProperties("demo")
public record MyConfigurationProperties(String myProperty) {
@ConstructorBinding
public MyConfigurationProperties(String myProperty) {
this.myProperty = Optional.ofNullable(myProperty).orElse("default");
}
}
java.util.Optional
属性:
@ConfigurationProperties("demo")
public record MyConfigurationProperties(Optional<String> myProperty) {
@ConstructorBinding
public MyConfigurationProperties(String myProperty) {
this(Optional.ofNullable(myProperty));
}
}
@Validated
和 java.util.Optional
的组合:
@Validated
@ConfigurationProperties("demo")
public record MyConfigurationProperties(@NotBlank String myRequiredProperty,
Optional<String> myProperty) {
@ConstructorBinding
public MyConfigurationProperties(String myRequiredProperty,
String myProperty) {
this(myRequiredProperty, Optional.ofNullable(myProperty));
}
}
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
?
回答我自己的问题。
上述错误源于 Spring 由于缺少无参数构造函数,Boot 无法构造 bean。记录为每个成员隐式声明一个带有参数的构造函数。
Spring Boot 允许我们使用 @ConstructorBinding
注释来启用 属性 构造函数绑定而不是 setter 方法(如 the docs and the answer to this question 中所述) .这也适用于记录,所以这有效:
@ConfigurationProperties("demo")
@ConstructorBinding
public record MyConfigurationProperties(
String myProperty
) {
}
更新:从 Spring Boot 2.6 开始,使用记录开箱即用,当记录只有一个构造函数时,不再需要 @ConstructorBinding
。见 release notes.
如果您需要以编程方式声明默认值:
@ConfigurationProperties("demo")
public record MyConfigurationProperties(String myProperty) {
@ConstructorBinding
public MyConfigurationProperties(String myProperty) {
this.myProperty = Optional.ofNullable(myProperty).orElse("default");
}
}
java.util.Optional
属性:
@ConfigurationProperties("demo")
public record MyConfigurationProperties(Optional<String> myProperty) {
@ConstructorBinding
public MyConfigurationProperties(String myProperty) {
this(Optional.ofNullable(myProperty));
}
}
@Validated
和 java.util.Optional
的组合:
@Validated
@ConfigurationProperties("demo")
public record MyConfigurationProperties(@NotBlank String myRequiredProperty,
Optional<String> myProperty) {
@ConstructorBinding
public MyConfigurationProperties(String myRequiredProperty,
String myProperty) {
this(myRequiredProperty, Optional.ofNullable(myProperty));
}
}