Skip to content

@ConfigurationProperties should have an option to fail fast for unresolvable properties #18816

Open
@anno1985

Description

@anno1985

When using placeholders in application.yaml (property files/externalised configuration) together with the @ConfigurationProperties annotation, there should be an option to have Spring fail fast at startup when a defined property is not found.

Example:

example.key: ${MY_ENV_VAR}
@ConfigurationProperties(prefix="example")
public class AcmeProperties {
  public String key;
  // Getters, setters, constructors omitted... 
}

Precondition:
No environment variable MY_ENV_VAR defined.

Current behaviour:
key above is populated with the verbatim String ${MY_ENV_VAR}

Expected/desired behaviour:
An exception is thrown while the application is starting up.

Potential cause:
Hardcoded flag in https://github.com/spring-projects/spring-boot/blob/v2.2.0.RELEASE/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/PropertySourcesPlaceholdersResolver.java#L51

Further information:
https://stackoverflow.com/q/58622189/2018047

NB:
Validating the String with @Validated after (failed) resolution is not the same as checking if resolution fails. Also, when no fail-fast is active, it might potentially be better to mirror System.getenv("MY_ENV_VAR")'s behaviour, and return null instead of the actual placeholder, ${MY_ENV_VAR}, verbatim.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions