Issue
I have many different services using spring-boot. I'd like to set up some configuration that is common for each, but allow the services to have their own properties and override them if they want. Example properties include spring.show_banner, management url ones, etc.
How can I do this? If I have the following:
- service-common with src/main/resources/application.yml with default properties
- service1 with src/main/resources/application.yml with its own properties
I'd like them to be merged with the service1 version taking precedence. Instead, it seems that only the first one found on the classpath is used.
(Alternatively, using @Configuration classes would be even better, but I'm not sure they can be used to define many of the properties)
Solution
There are several options available to you, all based on the order in which property sources are considered.
If your common library is responsible for creating the SpringApplication
it can use setDefaultProperties
. These values can be overridden by your services' application.properties
.
Alternatively, your library could use @PropertySource
on one of its @Configuration
classes to configure, for example, library.properties
as a source. Again, these properties could then be overriden in your services' application.properties
.
Answered By - Andy Wilkinson
Answer Checked By - Clifford M. (JavaFixing Volunteer)