Skip to content

@ConfigurationProperties on a @Bean method does not work when the bean has a constructor that is eligible for constructor binding #33710

@StijnArnauts

Description

@StijnArnauts

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.)

Metadata

Metadata

Assignees

Labels

type: regressionA regression from a previous release

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions