In 4.2. Externalized Configuration (Spring Boot 2.4.2) it states that
Config data files are considered in the following order: 1. Application properties packaged inside your jar (application.properties and YAML variants). 2. Profile-specific application properties packaged inside your jar (application-{profile}.properties and YAML variants). 3. Application properties outside of your packaged jar (application.properties and YAML variants). 4. Profile-specific application properties outside of your packaged jar (application-{profile}.properties and YAML variants).
However, what is unclear is which config values wins if you have both application.yml
and application.properties
on the same level. We'd like to use Variant 1 (see below), which seems to work, but relies on undocumented behavior that could easily change, so it would be great if the precedence handling could be added to the docs and checked with tests, if that is not already the case.
Context:
In SB <2.4 we used:
src/main/application.yml
to store profilessrc/main/application-default.properties
to setspring.profiles.include=stage-workstation
to activate a profile if nothing was activated via ENV/systemproperty. (We used the properties file as this key is invalid in yml, but using this file makes it also easy to override for test)src/test/application-default.properties
to setspring.profiles.include=stage-workstation, test
to activate the test profilesrc/main/application-test.yml
with profile in the name to avoid overriding theapplication.yml
For SB 2.4.x
We are evaluating two solutions
Variant 1
src/main/application.yml
to store profiles and usespring.profiles.group.default=stage-workstation
src/test/application.properties
to setspring.profiles.group.default=stage-workstation,test
to activate the test profile and use properties instead of yml to avoid overriding theapplication.yml
src/main/application-test.yml
with profile in the name to avoid overriding theapplication.yml
Variant 2
src/main/application.yml
to store profiles and usespring.profiles.group.default=stage-workstation
src/test/application.yml
that usesspring.config.import=file:${user.dir}/src/main/resources/application.yml
and overrides thespring.profiles.group.default=stage-workstation,test
(IMHO it is rather brittle to rely on${user.dir}
to be set to the correct location)
Comment From: philwebb
See #24719 for a recent regression where the order did change between Boot 2.3 and 2.4. Although it's still undocumented, we do at least now have a test.
Comment From: mbhave
The reason I think this isn't documented is because we thought people might stick with one format (either .yml
or .properties
) and not combine both. The use case described above is interesting because it lets you override spring.profiles.group
since that is no longer allowed in profile-specific file. I think we should document that .properties
will win in such a scenario.
Comment From: snicoll
Closing in favor of PR #25300