如何使用配置文件在 spring 中指定外部 application.yml [英] How to specify an external application.yml in spring with profiles
问题描述
来自 spring 文档 http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-yaml 我看到可以使用外部 YAML 文件.
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.
我能够使用 PropertyPlaceholderConfig 文件系统资源加载到 yaml 中,但它不支持活动配置文件.
I was able to use a PropertyPlaceholderConfig FileSystem resourse to load in yaml, but it did not honor the active profile.
我看到 application.properties 可以使用@PropertySource,但根据文档,这不适用于 YAML.
I saw for application.properties you can use @PropertySource, but that does not work for YAML according to the docs.
所以底线问题:如何在 Spring4/spring boot 中以配置文件感知方式指定 application.yml.
So bottom line question: How do a specify an application.yml in a profile aware fashion in Spring4/spring boot.
注意:它在 src/main/resources/application.yml 中有效
Note: It works in src/main/resources/application.yml
推荐答案
为了指定外部配置文件感知 .yml 文件,可以使用 SPRING_CONFIG_LOCATION 和 SPRING_PROFILES_ACTIVE 系统变量.
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 示例
JAVA_OPTS example
-Dspring.profiles.active=dev -Dspring.config.location=file:C:/application.yml
这将允许您在 YML 文件中提供多个配置文件,并让 spring 完成评估正确属性的繁重工作:
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
这篇关于如何使用配置文件在 spring 中指定外部 application.yml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!