springboot外部配置-特定于配置文件的配置 [英] springboot external configuration - profile specific configuration
问题描述
根据SpringBoot文档,配置顺序为:
According to the SpringBoot documentation, the order of configuration is as:
打包的jar之外的特定于配置文件的应用程序属性(application- {profile} .properties和YAML变体)
Profile-specific application properties outside of your packaged jar (application-{profile}.properties and YAML variants)
打包在jar中的特定于配置文件的应用程序属性(application- {profile} .properties和YAML变体)
Profile-specific application properties packaged inside your jar (application-{profile}.properties and YAML variants)
打包的jar之外的应用程序属性(application.properties和YAML变体).
Application properties outside of your packaged jar (application.properties and YAML variants).
打包在jar中的应用程序属性(application.properties和YAML变体).
Application properties packaged inside your jar (application.properties and YAML variants).
在我的项目中,我有一个名为"prod"的个人资料,以及以下文件:
On my project I have a profile called "prod" and the following files:
- application.yml(在jar内)
- application-prod.yml(在jar中)
我还想使用外部文件覆盖某些属性.由于根据文档,外部application.yml
将被内部application-prod.yml
覆盖,因此我需要确保将外部文件视为profile specific
配置文件.
And I also want to override some of the properties using an external file. Since according to the docs, an external application.yml
will be overridden by the internal application-prod.yml
, I need to make sure that the external file is considered as a profile specific
config file.
我尝试使用:
-Dspring.config.location=<my path>/application-prod.yml
而且我也尝试过:
-Dspring.config.location=file:<my path>/application-prod.yml
在所有情况下,我都从内部 application-prod.yml
In all cases I get the value from the internal application-prod.yml
如果我完全删除了内部配置文件,那么我会从外部获取值(所以我知道配置会拾取该文件).
If I totally remove the internal config file then I get the value from the external (so I know that the config picks up the file).
我了解到,该外部文件被视为与通用application.yml
等效,而不是特定于个人资料的文件.
I understand that this external file is considered as the equivalent to the generic application.yml
and not a profile specific.
如何将其配置为配置文件特定的外部配置?
How can I configure it to be considered as a profile specific external config?
推荐答案
找到了答案:
您需要从外部使用Directory
来设置特定于配置文件的配置文件,而不是直接使用该文件,并且该文件必须以/
结尾.因此必须是:
You need to use a Directory
externally to set the profile specific configuration files, not using the file directly and it needs to end in /
. So it has to be:
-Dspring.profiles.active=prod
-Dspring.config.location=/<some-path>/config/
(任何以/
结尾的路径)
-Dspring.config.location=/<some-path>/config/
(any path that ends in /
)
其中有一个:
application-prod.yml
这篇关于springboot外部配置-特定于配置文件的配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!