Multiple configuration tree to one class in spring boot

Thomas :

i have this in Application.yaml:

  override:
    email:
      enabled: true
      value: "[email protected]"
    phone:
      enabled: true
      value: "+420666666666"

How do I make a single class configuration with these values? I tried this:

public class RecipientOverrideConfig {

    @Configuration
    @ConfigurationProperties("override.email")
    @Data
    public class EmailOverride{

        Boolean enabled;
        String value;

    }

    @Configuration
    @ConfigurationProperties("override.phone")
    @Data
    public class SmsOverride{

        Boolean enabled;
        String value;

    }
}

But is there a better way to do this?

Thanks. :)

Lino :

I suggest making the whole class a ConfigurationProperties

@ConfigurationProperties("override")
public class RecipientOverrideProperties {
    private OverrideConfig email;
    private OverrideConfig phone;

    public class OverrideConfig {
        private Boolean enabled;
        private String value;
    }

    // getters and setters were omitted for brevity
}

And then autowire that into your configuration:

@Configuration
public class RecipientOverrideConfig {
    @Autowired // or even better, use constructor injection
    private RecipientOverrideProperties overrideProperties;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=237377&siteId=1