Say you have multiple configuration files you want to use to when starting the application. One with
some general settings shared by multiple instances and one with specific to this instance:
general.yaml:
app.yaml:
With @ConfigurationProperties you can automatically bind this configuration to a POJO:
Starting the app with these two configuration files is simply a matter of adding them to the spring.config.location
list:
Done and done! Off to the races.
Lists in Multiple Files
Let’s say you would like to have something like this:
general.yaml:
Then you have another file that that contains the passwords, perhaps generated from a key vault or something:
secrets.yaml:
Depending on the order/priority, either the name or the password will be null. Dang. Sadly this doesn’t work (as per
doc):
When a List is specified in multiple profiles, the one with the highest priority (and only that one)
So what now? Well you can rethink your design decisions do this some other way.
You still want to do this? Well one way would be to
disable @ConfigurationProperties, merge the multiple configuration sources into a single PropertySource and feed that to
the
Binder
manually.
Ok let’s first create a method that does the binding and merging for a generic prefix:
So now we can use this in a @Bean definition:
Now just remove @Configuration and @ConfigurationProperties annotations from your configuration class so the bean is
used to create the Config object.