Description
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 is bar
.
Given a new instance of Java Bean class FooBar
, the code should bind all environment variables FOO_
, then overwrite with all environment variables BAR_
.
public class FooBar {
private Duration latency = Duration.ofMillis(500L);
// other properties
// getters, setters
}
If there are no environment variables FOO_LATENCY
or BAR_LATENCY
, FooBar.getLatency()
is 500 ms. If only one of FOO_LATENCY
and BAR_LATENCY
is present, FooBar.getLatency()
takes its value. If both FOO_LATENCY
and BAR_LATENCY
are present, FooBar.getLatency()
takes the value of BAR_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.