通过合并 yml 文件启动 Spring 应用程序 [英] Starting Spring Application by merging yml files

查看:19
本文介绍了通过合并 yml 文件启动 Spring 应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

spring boot 有没有办法合并来自不同配置文件的属性并启动应用程序?

Is there a way in spring boot to merge properties from different config files and start the application?

例如:我的 application-local.yml 是默认使用的,具有以下属性

Ex: My application-local.yml which is the one that gets used by default had following properties

server:
  port: 8080
spring:
  profiles: local

propertyA: xxx
propertyB: yyy

现在不必从本地复制所有属性,而是像这样说 application-QA.yml

Now instead of having to copy all the properties from the local to lets say application-QA.yml like this

server:
  port: 8081
spring:
  profiles: local

propertyA: xxx
propertyB: zzz

其中只有端口 &propertyB 已更新,我可以使用以下内容吗?

where only port & propertyB has been updated, can I just have something like below?

application-QA.yml:

application-QA.yml:

server:
  port: 8081
propertyB: zzz

最后我希望有以下能力开始我的申请

At the end I want to have the following ability to start my applicatio

  • 使用本地 - ./gradlew bootrun应该从 application-local.yml 中获取属性,这就是现在发生的事情
  • 使用 QA - ./gradlew bootrun -Dsome.property=QA应该合并来自本地和 QA 的属性并启动应用程序

现在我们必须复制 port &propertyB 到 application-local.yml 并启动应用程序以指向 QA 环境,我想消除它.

Right now we have to copy port & propertyB to application-local.yml and start the application in order to point to QA environment and I would like to eliminate that.

注意:./gradlew bootrun -Dspring.profiles.active=QA 似乎对我不起作用,因为我需要 application-local.yml 中的所有属性都在 application-QA.yml 中,而不仅仅是我想覆盖的属性.

Note: ./gradlew bootrun -Dspring.profiles.active=QA doesn't seem to work for me since I will need all the properties in application-local.yml to be in application-QA.yml and not just the properties I want to override.

推荐答案

您可以使用弹簧配置文件来做到这一点.您可以同时激活多个配置文件,每个配置文件都会从附加的应用程序-.yml 中读取属性

You can do this using spring profiles. You can activate many profiles together and every profile will read properties from an additional application-.yml

  • 如果您激活配置文件本地",spring 将搜索文件应用程序-local.yml
  • 如果您激活配置文件QA",spring 将搜索文件应用程序-QA.yml

因此,您在 application-local.yml 中设置了通用属性,并且仅设置了不同的属性,例如application-QA.yml 中的端口.

So you set the common properties in application-local.yml, and only the different ones e.g. the port in application-QA.yml .

您同时激活两个配置文件

You activate both profiles together with

-Dspring.profiles.active=local,QA

另见 spring howto-属性和配置

这篇关于通过合并 yml 文件启动 Spring 应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆