-
Notifications
You must be signed in to change notification settings - Fork 41.2k
@ConfigurationProperties should have an option to fail fast for unresolvable properties #18816
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I believe we intentionally set |
@mbhave also tracked down some internal discussion we had about this last time around: From @wilkinsona
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ConfigurableApplicationContext;
import com.example.demo.PlaceholderChangeApplication.ExampleProperties;
@SpringBootApplication
@EnableConfigurationProperties(ExampleProperties.class)
public class PlaceholderChangeApplication {
public static void main(String[] args) {
ConfigurableApplicationContext context =
SpringApplication.run(PlaceholderChangeApplication.class,
"--com.example.alpha=Value with ${placeholder}");
System.out.println(context.getBean(ExampleProperties.class).getAlpha());
}
@ConfigurationProperties(prefix="com.example")
static class ExampleProperties {
private String alpha;
public String getAlpha() {
return alpha;
}
public void setAlpha(String alpha) {
this.alpha = alpha;
}
}
}
|
I think we intentionally rolled this back in 2.0 because the major upgrade was quite onerous already and we didn't want to add more friction. |
we also desire this feature. it's easier to tell that a server won't start up if the process fails than if it goes into the current spring retry loop. lacking this feature directly: are there any alternative paths to the same result? |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Ops, shooting up legs in production... |
Just not to be rude, i've come up with a workaround: you could use validation over your Configuration properties class: @Data
@Component
@Validated
@ConfigurationProperties("my-config-props")
public class MyConfigProps {
@NotBlank
private String host;
} application.yaml: my-config-props:
host: ${MY_CONFIG_PROPS_HOST:} |
hi, just a reminder that this problem still exists with Spring Boot 3.2. There is no easy configuration flag for enabling/disabling exception-behaviour for not resolvable ENV during configuration property binding. This missing feature causes some headaches/work around on our code side when implementing fail-fast failure detection @philwebb maybe I am thinking to "simple", but why not adding a System-Property as feature toggle ? Something like (my suggestion)
instead of a hardcoded flag (current implementation)
|
Is this open to contributions? Second time I'm having an issue that I would have been caught with Not having a fail-fast behaviour for this makes it harder to spot configuration errors at boot time, and more likely that they will cause production bugs further down the line. |
I truly hope that this Fail Fast feature will be available someday, and I don’t understand why it isn’t just the standard behavior. |
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:
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 mirrorSystem.getenv("MY_ENV_VAR")
's behaviour, and returnnull
instead of the actual placeholder,${MY_ENV_VAR}
, verbatim.The text was updated successfully, but these errors were encountered: