Gourav
Gourav

Reputation: 55

Spring Bean Self autowiring via ConfigurationProperties

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

Upvotes: 0

Views: 132

Answers (2)

Dirk Deyne
Dirk Deyne

Reputation: 6936

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
}

Upvotes: 1

MalaLednica
MalaLednica

Reputation: 11

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

Upvotes: 0

Related Questions