-
Notifications
You must be signed in to change notification settings - Fork 41.2k
Provide a way to bind classes with multiple prefixes #26144
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
The answer on Stack Overflow can be simplified a little bit as you're binding to an existing instance: FooBar fooBar = new FooBar();
binder.bind("x", Bindable.ofInstance(fooBar));
binder.bind("y", Bindable.ofInstance(fooBar)); It's obviously subjective, but that reads quite nicely to my eyes. I think merging results would look something like this: FooBar fooBar = new FooBar();
BindResult<FooBar> x = binder.bind("x", Bindable.ofInstance(fooBar));
BindResult<FooBar> y = binder.bind("y", Bindable.ofInstance(fooBar));
fooBar = x.mergeWith(y).get(); That's slightly more verbose and I don't find it any more readable. Binding multiple prefixes would look something like this: FooBar fooBar = new FooBar();
binder.bind(Arrays.asList("x", "y"), Bindable.ofInstance(fooBar)); That's more concise than binding twice, but I find it less readable. It's not as clear to me how the two prefixes will be handled. Let's see what the rest of the team thinks. |
One benefit of the multiple prefixes approach is that it would support constructor binding to an immutable type. |
IMO, the inherent ordering in a list is a good indication that the prefixes are handled as shown. |
It can also be done with
|
Not so easily unfortunately. The The |
One other thing we'll need to consider is merging rules for |
Spring Cloud would also like this feature. |
See also #7986 |
We discussed this with the Spring Cloud team and they were able to use a different approach to support their use-case. We think it's best not to add multiple prefix support to the binder at this time, given how complex the code is and that there is way to achieve the fallback, if necessary, by calling bind multiple times. |
I've a use case where I need to bind configuration properties based on two prefixes, one of which is determined at runtime. Let's say the constant prefix is
foo
and the runtime prefix isbar
.Given a new instance of Java Bean class
FooBar
, the code should bind all environment variablesFOO_
, then overwrite with all environment variablesBAR_
.If there are no environment variables
FOO_LATENCY
orBAR_LATENCY
,FooBar.getLatency()
is 500 ms. If only one ofFOO_LATENCY
andBAR_LATENCY
is present,FooBar.getLatency()
takes its value. If bothFOO_LATENCY
andBAR_LATENCY
are present,FooBar.getLatency()
takes the value ofBAR_LATENCY
.This SO answer shows a way to do it by repeated binding. It works, but it confusing.
It'd be nice to be able to merge
BindResult
s. Some ideas off the top of my head:binder.bind
accepts multiple prefixes, not just one, latter progressively overwriting the former.bindResult.mergeWith(anotherBindResult)
, latter overwriting the former.The text was updated successfully, but these errors were encountered: