Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot and external configurations

I am trying to make a Spring Boot application. Everything is fine once I deploy to the fat jar file with everything contained in it. But, what I actually want is the configuration files to be located externally. for example I have the following directory structure:

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

If I use this directory structure and execute the jar using

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 maintaining the directory structure above? Or, what is the alternative/best practice?

like image 302
dondragon2 Avatar asked Aug 17 '14 02:08

dondragon2


People also ask

Do we need @configuration in spring boot?

One of the most important annotations in spring is @Configuration annotation which indicates that the class has @Bean definition methods. So Spring container can process the class and generate Spring Beans to be used in the application. This annotation is part of the spring core framework.

What are possible sources of external configuration?

This way, a configuration can be provided from different sources, for example, file systems, the internet, external services, configuration services, vaults, etc.


2 Answers

Boot external config is what you are looking for.

Especially it mentions:

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

  • A /config subdir of the current directory.
  • The current directory
  • A classpath /config package
  • The classpath root

So I would say adding the config folder on classpath is good step. Them it should find application.properties and load it automatically.

For different config files I use:

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

Edit: As Dave pointed out (Thank Dave!) there is either -cp or -jar, so you can't add it to classpath like that. But there are options. This should help you to solve the problem: Call "java -jar MyFile.jar" with additional classpath option.

Additionally @PropertySource doesn't require the resources to be classpath resources if I'm not mistaken.

like image 121
Jan Zyka Avatar answered Oct 19 '22 23:10

Jan Zyka


It should also be mentioned that there is a spring.config.location parameter that allows one to specify a file system / classpath location for externalized configuration files. This is documented 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

like image 23
btiernay Avatar answered Oct 20 '22 00:10

btiernay