When adding an extra constructor to a @ConfigurationProperties
annotated record in Spring Boot, you need to explicitly tell Spring Boot which constructor to use for binding by annotating it with @ConstructorBinding
.
@ConfigurationProperties("user")
record User(String name, int age) {
@ConstructorBinding
public User { // Annotate the canonical constructor
}
public User(String name) {
this(name, 0);
}
}
@ConstructorBinding
is required when multiple constructors exist to avoid ambiguity.