Spring Boot and External Configurations

I am trying to create a Spring boot application. Everything is fine as soon as I deploy to a file with a bold flag with everything that is contained in it. But actually I want the configuration files to be outside. For example, I have the following directory structure:

bin - contains startup and shutdown scripts conf - all configurations. ie application.properties, logback.xml i18n.properties logs - log files libs - app.jar 

If I use this directory structure and run the jar with

 java -cp ./conf -jar ../libs/app.jar 

then the properties in the conf directory are not loaded or recognized. Is there a better way to do this while maintaining the directory structure above? Or what is the alternative / best practice?

+5
source share
2 answers

Download the external configuration - this is what you are looking for.

He especially mentions:

SpringApplication will load the properties from the application.properties files in the following places and add them to the Spring Environment:

  • A / config subdirectory of the current directory.
  • Current directory
  • Classpath / config package
  • Classpath root

So, I would add that adding a config folder to the class path is a good step. He should find application.properties and download it automatically.

For different configuration files, I use:

 @Configuration @EnableAutoConfiguration @PropertySource({ "classpath:path/some.properties", "classpath:another/path/xmlProperties.xml" }) public class MyConfiguration { // ... } 

Edit: As Dave noted (Thank Dave!), There is either -cp or -jar, so you cannot add it to the classpath. But there are options. This should help you solve the problem: Call & java -jar MyFile.jar "with the optional classpath option .

In addition, @PropertySource does not require that resources be classpath resources, if I am not mistaken.

+7
source

It should also be noted that there is a spring.config.location parameter that allows you to specify the location of the file system / class for external configuration files. This is described in the following section of the Spring Boot Reference Guide:

http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-application-property-files

+2
source

Source: https://habr.com/ru/post/1200502/


All Articles