Thomas
Thomas

Reputation: 211

Multiple configuration tree to one class in spring boot

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?

Upvotes: 0

Views: 673

Answers (1)

Lino
Lino

Reputation: 19910

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;
}

Upvotes: 1

Related Questions