Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set default properties in a library with spring-boot

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)

like image 471
Eric D Avatar asked Mar 06 '15 05:03

Eric D


People also ask

How do I apply application properties in spring boot?

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.

How do I override application properties in spring boot?

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.

Where should I place application properties in spring boot?

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.


1 Answers

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.

like image 199
Andy Wilkinson Avatar answered Sep 25 '22 02:09

Andy Wilkinson