Skip to content

Add support for configuration property fallback #7986

Closed
@philwebb

Description

@philwebb
Member

Some way to specify the default value for interfaces and a different fallback property.

Activity

added this to the 2.0.0.M1 milestone on Jan 13, 2017
snicoll

snicoll commented on Jan 13, 2017

@snicoll
Member

An example

@ConfigurationProperties("foo.bar")
public class BarProperties {

    private int timeout 1000;

    @ConfigurationPropertyValue(fallback = "foo.default.timeout")
    public int getTimeout() { return timeout; }
  
   public void setTimeout(int timeout) { ... }  

}

The same annotation can be used to provide default values for interface-based configuration properties, something like

@ConfigurationProperties("foo.bar")
public interface BarProperties {

    @ConfigurationPropertyValue(fallback = "foo.default.timeout", default = "1000")
    int getTimeout();

}
afranken

afranken commented on Jan 24, 2017

@afranken

instead of (or in addition to) adding an annotation that works for interfaces for properties classes, you should add an annotation for fields of properties classes (much like the @Autowired annotation) and not force users to add public getter and setter methods to properties classes.

wilkinsona

wilkinsona commented on Jan 24, 2017

@wilkinsona
Member

@afranken Please see #1254

thombergs

thombergs commented on Feb 19, 2017

@thombergs
Contributor

In addition to defining a fallback, I would like to define whether it's allowed for a configuration property to be missing. Most of the times, startup of the application should fail when an expected property is missing. With @ConfigurationProperties alone, I currently see no possibility to fail on startup if a property is missing (other than creating a @PostConstruct method or something similar that does manual validation). It will just be assigned NULL or if it's a primitive the default value of that primitive type.

So, essentially, it would be great to be able to define exceptionIfMissing (in the style of exceptionIfInvalid on @ConfigurationProperties) on the annotation proposed by @snicoll like this:

@ConfigurationProperties("foo.bar")
public class BarProperties {

   private int optionalTimeout;
   private int mandatoryTimeout;

   @ConfigurationPropertyValue(exceptionIfMissing=false)
   public int getOptionalTimeout() { return optionalTimeout; }
  
   @ConfigurationPropertyValue(exceptionIfMissing=true)
   public int getMandatoryTimeout() { return mandatoryTimeout; }
  
  ...
}

What are your thoughts on this?

afranken

afranken commented on Feb 19, 2017

@afranken

@thombergs I would expect that JSR-303 validation will keep working as it does right now:
https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-validation

In your example, you would annotate your Integer fields with for example @Max or @Min, and @NotNull if you wanted an exception to be thrown during startup...

thombergs

thombergs commented on Feb 19, 2017

@thombergs
Contributor

Ah, I didn't read the docs careful enough. Bean Validation works fine for my proposed use case. Thanks for the pointer.

removed this from the 2.0.0.M1 milestone on May 16, 2017

35 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: declinedA suggestion or change that we don't feel we should currently apply

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @ammmze@snicoll@philwebb@afranken@wilkinsona

        Issue actions

          Add support for configuration property fallback · Issue #7986 · spring-projects/spring-boot