Home > Net >  Spring Bean Self autowiring via ConfigurationProperties
Spring Bean Self autowiring via ConfigurationProperties

Time:01-07

I am trying to directly populate the configuration object from the YAML file using @ConfigurationProperties. But when I checked the person's object, name and age got populated but the child is null. I am using the spring boot 2.7.4 version. Is this not supported by spring or any other way to handle such a situation?

@ConfigurationProperties("config")
@Component
public class PersonConfiguration {

    private Person person;

    //setter getters
}

public class Person {
    private String name;
    private int age;
    @NestedConfigurationProperty
    private Person child;

//setter getters of all 3 
}

config:
  person:
    name: "my name"
    age: 40
    child:
      name: "child1"
      age: 14

CodePudding user response:

It seems like your child definition in config file doesn´t represent the Person class structure, it is missing child attribute. Try this: config: person: name: "my name" age: 40 child: name: "child1" age: 14 child: null

CodePudding user response:

You should not need PersonConfiguration

This will work:

@Configuration
@ConfigurationProperties("config.person")
public class Person {
    private String name;
    private int age;
    private Person child;
   
    //setter and getters
}
  • Related