-
Notifications
You must be signed in to change notification settings - Fork 41.5k
Description
Using Spring Boot 3.0.1, I have the following POJO on which I wish to bind a certain property:
public class TestProperties {
private String prop1;
private String prop2;
public TestProperties(String prop1, String prop2) {
this.prop1 = prop1;
this.prop2 = prop2;
}
public String getProp1() {
return prop1;
}
public void setProp1(String prop1) {
this.prop1 = prop1;
}
public String getProp2() {
return prop2;
}
public void setProp2(String prop2) {
this.prop2 = prop2;
}
}
Note that this is a class from a third-party library not under my control. I need to make this available as a Bean, and I wish to set a property on it:
# (in application.properties) testprop.prop1=something
@Bean
@ConfigurationProperties(prefix = "testprop")
public TestProperties testProperties() {
return new TestProperties("1", "2");
}
The previous code fails silently, and my property is not set.
It seems as if the new Spring Boot 3 rules regarding automatic ConstructorBinding have something to do with this. If I remove the parameterized constructor from the TestProperties
class, or if I add a second constructor, the problem goes away and my property is set correctly. However, since I return an already constructed object from the Bean method, I would expect Spring Boot to always use setter binding instead of constructor binding.
(This is a simplified example. In reality, TestProperties
is constructed using a builder, and the parameterized constructor is package private, intended to be used only by that builder.)