Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spring boot: add new yml files to application config

i want developers to be able to locally override some configuration properties. (let's say we work on google drive and everyone should test it on its own account). i don't want to override properties using command line (because it has to be set inside every IDE configuration and on every CLI run).

what i want is: application should use all the standard spring boot config files (application.yml etc) and also look for e.g. local.yml (on the classpath) or some file inside user.home. and those additional files should override other settings.

how to add new yml resources and order them correctly?

edit: i know spring's default orders and locations. question is about adding new ones

like image 496
piotrek Avatar asked Mar 29 '16 20:03

piotrek


People also ask

Can we have multiple YAML file in spring boot?

Note: You can refer to multiple . yml files this way if you want. You need additional . yml files and Config classes like below example.


2 Answers

If you look in the Spring Boot documentation about the locations for configuration files (http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-external-config), you can see, that they are loaded from the following places (amongst others):

  • Profile-specific application properties outside of your packaged jar (application-{profile}.properties and YAML variants)
  • Application properties outside of your packaged jar (application.properties and YAML variants).

There are two default locations where they are loaded from ( see http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-external-config-application-property-files):

  • A /config subdirectory of the current directory.
  • The current directory

Current directory in this case means the working directory for the Java process (Usually the directory where the JAR is located, or in case of running with in the IDE, usually the project root folder). So the developers just can place their own configuration files in that places and they are automatically loaded (and will override properties within the JARs). Add that files to .gitignore (or .svnignore or ...) and they won't accidentally committed into your repository.

like image 55
dunni Avatar answered Oct 04 '22 00:10

dunni


There's a new way to do this, after Spring Boot v2.4, by using spring.config.import: https://spring.io/blog/2020/08/14/config-file-processing-in-spring-boot-2-4#importing-additional-configuration

By adding this part to your application.yml file, you should be able to import the additional configuration:

spring:
  config:
    import: local.yml

The article also has this section:

Imports can be considered as additional documents inserted just below the document that declares them. They follow the same top-down ordering as regular multi-document files: An import will only be imported once, no matter how many times it is declared.

So the contents of local.yml should be handled as if they were appended to the end of application.yml, thereby allowing you to override any property in application.yml.

like image 34
loket Avatar answered Oct 03 '22 23:10

loket