14
votes

From the spring documentation http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-yaml i see that an external YAML file is possible.

I was able to use a PropertyPlaceholderConfig FileSystem resourse to load in yaml, but it did not honor the active profile.

I saw for application.properties you can use @PropertySource, but that does not work for YAML according to the docs.

So bottom line question: How do a specify an application.yml in a profile aware fashion in Spring4/spring boot.

Note: It works in src/main/resources/application.yml

1
By just placing a application-[profile].yml next to the application.yml. As explained at the same link you refer to. Spring Boot will automatically load it. - M. Deinum
That works for an in class path resource using a standard properties file. My goal is an external recourse yaml (all profiles in one file) - redwhite
Please read the docs instead of creating your own interpretation. It works for YAML files as well. Next to that it also works for files in the same directory as your jar or a config directory. So again read that configuration section... If you want to specify a file just start your application with --spring.config.location which is explained a little further down that section (docs.spring.io/spring-boot/docs/current/reference/html/…). - M. Deinum
I have read the docs many times, but all the same thank you for your response. The missing piece was specifying the file: prefix in my spring.config.location variable and it now works. - redwhite
@redwhite if you can point me to any public github repository addressing this problem, it'd be very helpful. - bijayshrestha

1 Answers

19
votes

In order to specify an external profile aware .yml file the SPRING_CONFIG_LOCATION and SPRING_PROFILES_ACTIVE system variables can be used.

JAVA_OPTS example

-Dspring.profiles.active=dev -Dspring.config.location=file:C:/application.yml

This will allow you to have provide multiple profiles inside of a YML file and let spring do the heavy lifting of evaluating the correct properties:

spring:
  profiles: dev
someprop: devprop
---
spring:
  profiles: test
someprop: testprop