java 特定环境的spring yml文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37033129/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 02:08:04  来源:igfitidea点击:

spring yml file for specific environment

javaspringspring-bootyaml

提问by Amar Dev

I have 3 ymlfiles namely

我有3个yml文件

  • application-default.yml-> default properties, should be available in all profiles
  • application-dev.yml-> properties only for dev profile
  • application-prod.yml-> properties only for prod profile
  • application-default.yml-> 默认属性,应该在所有配置文件中可用
  • application-dev.yml-> 仅用于开发配置文件的属性
  • application-prod.yml-> 仅适用于 prod 配置文件的属性

When I start my boot application by passing the -Dspring.profiles.active=dev,I am able to access the application-dev.ymlspecific properties. But I cant get the properties defined in the application-default.ymlfiles. Following is my application-dev.ymlfile:

当我通过传递 启动我的启动应用程序时-Dspring.profiles.active=dev,我能够访问application-dev.yml特定的属性。但我无法获得application-default.yml文件中定义的属性。以下是我的application-dev.yml文件:

Spring:
 profiles:
  include: default

spring.profiles: dev

prop:
 key:value

采纳答案by Amar Dev

I was able to solve my problem, here is what I did.

我能够解决我的问题,这就是我所做的。

Created a file application-common.yml, put the common properties there. Then in the application-{env}.yml files I put this on the top.

创建了一个文件 application-common.yml,把公共属性放在那里。然后在 application-{env}.yml 文件中我把它放在最上面。

spring:
 profiles:
  include: default

Since I dont need to ever load the default profile specifically, this works for me!!!

由于我不需要专门加载默认配置文件,这对我有用!!!

回答by Daniel Olszewski

TL;DR

TL; 博士

Just rename the application-default.ymlfile to application.ymland will work as you expect.

只需重命名该应用程序default.yml文件application.yml和您预期会工作。

Explanation

解释

According to the description in the docs, a file called application-{suffix}.ymlis activated when you run your application with the profile which name matches with the suffix. In addition, the main application.ymlis loaded by default so it's the perfect place to put common properties for all profiles. Alternatively, if you want to keep the name of your file as application-default.yml you can pass two profiles to your Spring Boot application:

根据文档中的描述,当您使用名称与后缀匹配的配置文件运行应用程序时,会激活名为application-{suffix}.yml的文件。此外,主application.yml是默认加载的,因此它是放置所有配置文件通用属性的理想场所。或者,如果您想将文件名称保留为 application-default.yml,您可以将两个配置文件传递给您的 Spring Boot 应用程序:

-Dspring.profiles.active=default,dev

This way you will activate two profiles and both properties files will be loaded.

这样您将激活两个配置文件,并且将加载两个属性文件。

回答by WesternGun

What I do is:

我要做的是:

Put common settings in application.xml, and in this file add:

将常用设置放入application.xml,并在此文件中添加:

spring:
  profiles:
    active: dev, pro, xxx...

all the profiles you want to activate.

您要激活的所有配置文件。

So that you just edit this file to switch environment.

这样您只需编辑此文件即可切换环境。

Remember that external files procedes, so you can leave another application.xmloutside of the WAR to activate dev/pro/... environment instead of editing this file every time. Be sure to check the documentation:

请记住,外部文件会继续运行,因此您可以application.xml在 WAR 之外保留另一个文件来激活dev/ pro/... 环境,而不是每次都编辑此文件。请务必检查文档:

https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html

https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html