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:
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)
Reading Application Properties with @ConfigurationProperties Another way to read application properties in Spring Boot app is to use the @ConfigurationProperties annotation. To do that we will need to create a Plain Old Java Object where each class field matches the name of the key in a property file.
To override your Spring Boot application properties when it's running on Kubernetes, just set environment variables on the container. To set an environment variable on a container, first, initialise a ConfigMap containing the environment variables that you want to override.
You will need to add the application. properties file in your classpath. If you are using Maven or Gradle, you can just put the file under src/main/resources . If you are not using Maven or any other build tools, put that under your src folder and you should be fine.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With