Vikdor
Vikdor

Reputation: 24134

how to load spring-boot based REST application properties from a different file?

It seems the spring-boot launcher looks for application.{xml|properties|yml|yaml} file in the following locations to load properties. Is there a way to specify a different filename or file to load properties from?

$ java -Ddebug -jar target/app-1.0.jar
16 [main] DEBUG org.springframework.boot.context.config.ConfigFileApplicationListener  - Skipped config file 'file:./config/application.properties' resource not found
16 [main] DEBUG org.springframework.boot.context.config.ConfigFileApplicationListener  - Skipped config file 'file:./config/application.yaml' resource not found
16 [main] DEBUG org.springframework.boot.context.config.ConfigFileApplicationListener  - Skipped config file 'file:./config/application.yml' resource not found
32 [main] DEBUG org.springframework.boot.context.config.ConfigFileApplicationListener  - Skipped config file 'classpath:/application.properties' resource not found
32 [main] DEBUG org.springframework.boot.context.config.ConfigFileApplicationListener  - Skipped config file 'classpath:/application.xml' resource not found
32 [main] DEBUG org.springframework.boot.context.config.ConfigFileApplicationListener  - Skipped config file 'classpath:/application.yaml' resource not found
32 [main] DEBUG org.springframework.boot.context.config.ConfigFileApplicationListener  - Skipped config file 'classpath:/application.yml' resource not found
32 [main] DEBUG org.springframework.boot.context.config.ConfigFileApplicationListener  - Skipped config file 'classpath:/config/application.properties' resource not found
32 [main] DEBUG org.springframework.boot.context.config.ConfigFileApplicationListener  - Skipped config file 'classpath:/config/application.xml' resource not found
32 [main] DEBUG org.springframework.boot.context.config.ConfigFileApplicationListener  - Skipped config file 'classpath:/config/application.yaml' resource not found
32 [main] DEBUG org.springframework.boot.context.config.ConfigFileApplicationListener  - Skipped config file 'classpath:/config/application.yml' resource not found
32 [main] DEBUG org.springframework.boot.context.config.ConfigFileApplicationListener  - Skipped config file 'file:./application.properties' resource not found
32 [main] DEBUG org.springframework.boot.context.config.ConfigFileApplicationListener  - Skipped config file 'file:./application.xml' resource not found
32 [main] DEBUG org.springframework.boot.context.config.ConfigFileApplicationListener  - Skipped config file 'file:./application.yaml' resource not found
32 [main] DEBUG org.springframework.boot.context.config.ConfigFileApplicationListener  - Skipped config file 'file:./application.yml' resource not found
6 [main] DEBUG org.springframework.boot.context.config.ConfigFileApplicationListener  - Skipped config file 'file:./config/application.xml' resource not found

EDIT:

Is there a way to specify another Bean configuration file (like application-context.xml)?

Upvotes: 3

Views: 2739

Answers (3)

tmarwen
tmarwen

Reputation: 16374

In command line you can use below property to mention an additional boot configuration file:

--spring.config.name="file:/path/to/application.properties"

An alternative would be:

-Dspring.config.name="file:/path/to/application.properties"

Note that characters are lower case and the word separator is a period ..

Otherwise you can use an environment variable with key you used already:

  • In a *nix system:

    export SPRING_CONFIG_NAME=file:/path/to/application.properties
    
  • In Windows OS:

    set SPRING_CONFIG_NAME=file:/path/to/application.properties
    

More on Spring Boot Configuration in following Docs resource.

Upvotes: 1

mjspier
mjspier

Reputation: 6526

You can set the folder where springs searches for the configuration file during startup.

java -jar target/app-1.0.jar -Dspring.config.location=your/config/dir/

Upvotes: 2

Mithun
Mithun

Reputation: 8067

You need to use @ConfigurationProperties to load the properties.

@ConfigurationProperties(locations = {"yourProperties.xml"})

Upvotes: 3

Related Questions