Set default properties in a library with spring-boot
I am not sure what you mean by merging them.
But I'm assuming that in the end, you are describing the situation where you have profile-specific configuration. Because, any properties that are specific to a certain service can be managed/injected using Spring profiles, which will always take precedence over default property files (see documentation).
For example, you can have the file application-service1.properties
which would automatically be used when you run your app with the property spring.profiles.active=service1
, which can be specified in the command line and other places.
If you don't specify this property, Spring Boot will fallback to the default application.properties
file.
And you can of course write the common properties in both files:
application.properties:
service.url=http://localhost:8080/endpoint
service.user=admin
service.password=admin
application-service1.properties:
service.url=http://api.service.com/endpoint
service.user=admin
service.password=aosdnoni3
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
.