I'm trying to set up some default properties across multiple Spring Boot projects, where the projects will be using my library as a bundled jar file.
I see there's a spring.config.additional-location setting but it loads those properties after the ones in application-(profile).yml which is the opposite of what I want; I want to specify defaults-(profile).yml in my project and have all of the other projects inherit those as their defaults with the ability to override them in application.yml (or application.properties, or application-(profile).yml, or from the command line, etc.). It's imperative that all of the default Spring Boot property loading works the same way it does now with the exception of being able to load these additional profile-specific defaults.
How can I do this? Neither Google nor ChatGPT were of any help.
EDIT: In case it helps, my library is Spring Boot enabled (or whatever the term is): It automatically configures beans that all of my clients use.
I'm trying to set up some default properties across multiple Spring Boot projects, where the projects will be using my library as a bundled jar file.
I see there's a spring.config.additional-location setting but it loads those properties after the ones in application-(profile).yml which is the opposite of what I want; I want to specify defaults-(profile).yml in my project and have all of the other projects inherit those as their defaults with the ability to override them in application.yml (or application.properties, or application-(profile).yml, or from the command line, etc.). It's imperative that all of the default Spring Boot property loading works the same way it does now with the exception of being able to load these additional profile-specific defaults.
How can I do this? Neither Google nor ChatGPT were of any help.
EDIT: In case it helps, my library is Spring Boot enabled (or whatever the term is): It automatically configures beans that all of my clients use.
Share edited Mar 7 at 13:21 Blake M. asked Mar 6 at 21:08 Blake M.Blake M. 1241 silver badge11 bronze badges1 Answer
Reset to default 0Here is how I would do it:
- In your library define a property file - e.g.
default-properties.yml
:
some-property=foo
- In your client projects (that have that library as a dependency, import the property source):
@PropertySource("classpath:default-properties.yml")
@SpringBootApplication
public class FooApplication {}
now if your client project doesn't define the property some-property
its default value is foo
, if your client project does define it - it will get overwritten.
This works because @PropertySource
has a lower order of evaluation that the properties files, therefore property files can overwrite it - see externalized configuration