Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify an external application.yml in spring with profiles

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

like image 669
redwhite Avatar asked Sep 03 '15 21:09

redwhite


People also ask

How do I keep my Spring Boot profiles different?

The solution would be to create more property files and add the "profile" name as the suffix and configure Spring Boot to pick the appropriate properties based on the profile. Then, we need to create three application. properties : application-dev.

How do you externalize a constants from a Spring configuration file into a properties file?

You can use properties files, YAML files, environment variables and command-line arguments to externalize configuration. Property values can be injected directly into your beans using the @Value annotation, accessed via Spring's Environment abstraction or bound to structured objects.


1 Answers

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
like image 176
redwhite Avatar answered Sep 21 '22 14:09

redwhite