-
Notifications
You must be signed in to change notification settings - Fork 41.1k
Add support for configuration property fallback #7986
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
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();
} |
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 |
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 So, essentially, it would be great to be able to define @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? |
@thombergs I would expect that JSR-303 validation will keep working as it does right now: In your example, you would annotate your |
Ah, I didn't read the docs careful enough. Bean Validation works fine for my proposed use case. Thanks for the pointer. |
…ng-projects#7986). Added onNull() to BindHandler to be able to react to properties that bind to null to choose a fallback value instead. ConfigurationPropertyValue annotations (which allow defining a fallback value for a property) are now collected by ConfigurationPropertyValueReader and a map of fallback properties is passed into a FallbackBindHandler. The handler then reacts on a null value by providing the fallback value instead of the null value.
…ng-projects#7986). Added onNull() to BindHandler to be able to react to properties that bind to null to choose a fallback value instead. ConfigurationPropertyValue annotations (which allow defining a fallback value for a property) are now collected by ConfigurationPropertyValueReader and a map of fallback properties is passed into a FallbackBindHandler. The handler then reacts on a null value by providing the fallback value instead of the null value.
properties must always be from the same namespace (spring-projects#7986).
…ng-projects#7986). Added onNull() to BindHandler to be able to react to properties that bind to null to choose a fallback value instead. ConfigurationPropertyValue annotations (which allow defining a fallback value for a property) are now collected by ConfigurationPropertyValueReader and a map of fallback properties is passed into a FallbackBindHandler. The handler then reacts on a null value by providing the fallback value instead of the null value.
The check has been removed so that fallback properties may come from a different namespace than the original property (spring-projects#7986).
I gave this feature a bit more thought and it looks like we could use a clear split between two completely different features:
Let's focus on 1. It feels to me the #1 use case is an object that gathers some default and then some Map of “stuff” with fallback on those defaults. As we aren't generating metadata for maps that fallback information will not end up in the metadata. I think that can be a problem. Also if the fallback key has a fallback on its own you can easily create a cycle. It would be wise to allows only one level of fallback but given the metadata generation is completely decentralized, there is no way to prevent that to happen. I am rephrasing the scope of this issue to be 1. |
…ng-projects#7986). Added onNull() to BindHandler to be able to react to properties that bind to null to choose a fallback value instead. ConfigurationPropertyValue annotations (which allow defining a fallback value for a property) are now collected by ConfigurationPropertyValueReader and a map of fallback properties is passed into a FallbackBindHandler. The handler then reacts on a null value by providing the fallback value instead of the null value.
properties must always be from the same namespace (spring-projects#7986).
Added onNull() to BindHandler to be able to react to properties that bind to null to choose a fallback value instead. ConfigurationPropertyValue annotations (which allow defining a fallback value for a property) are now collected by ConfigurationPropertyValueReader and a map of fallback properties is passed into a FallbackBindHandler. The handler then reacts on a null value by providing the fallback value instead of the null value. Removed checking for descendant properties, because otherwise fallback properties must always be from the same namespace. Fixes spring-projects#7986
This is thread is pretty old now, but would it be better to have @ConfigurationProperties("foo.bar")
public interface BarProperties {
@ConfigurationPropertyValue(default = "${foo.default.timeout:1000}")
int getTimeout();
} |
We're going to be looking at immutable configuration properties so I think it's best to close this for now so we don't make that work more complicated. Once we've got immutable support in we can reconsider. |
Some way to specify the default value for interfaces and a different fallback property.
The text was updated successfully, but these errors were encountered: