Java Spring Boot:如何使用多个 yml 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23134869/
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-08-13 20:58:35  来源:igfitidea点击:

Spring Boot: how to use multiple yml files

javaspringyamlspring-boot

提问by Johan Frick

In Spring Boot, I know that I can replace application.properties with application.yml and use the YAML format. However, my application.yml is getting crowded so I need to split it up a bit. How can I do that? I would like to do something like this:

在 Spring Boot 中,我知道我可以用 application.yml 替换 application.properties 并使用 YAML 格式。但是,我的 application.yml 越来越拥挤,所以我需要将其拆分一下。我怎样才能做到这一点?我想做这样的事情:

...
@Configuration
@EnableAutoConfiguration
@EnableWebMvc
@EnableScheduling
@PropertySource({"classpath:application.yml", "classpath:scheduling.yml"})
public class ApplicationConfig {
...

采纳答案by Dave Syer

@PropertySourcedoes not support YAML (probably it will in Spring 4.1). You can set spring.config.locationor spring.config.nameto a comma-separated list (e.g. as System property or command line argument).

@PropertySource不支持 YAML(可能会在 Spring 4.1 中支持)。您可以将spring.config.location或设置spring.config.name为逗号分隔的列表(例如作为系统属性或命令行参数)。

Personally I like all my YAML in the same place (the structure really helps to break it up visually, and you can use documents inside the file to split it up more). That's just taste I guess.

就我个人而言,我喜欢将所有 YAML 放在同一个地方(这种结构确实有助于在视觉上对其进行拆分,并且您可以使用文件中的文档将其拆分得更多)。我猜那只是味道。

回答by Jalal Sajadi

You can use active profile concept in your main yaml file. For example:

您可以在主 yaml 文件中使用活动配置文件概念。例如:

spring.profiles.active: test

that means you should have application-test.ymlfile in your resource directory. Consider that active profiles will override properties with the same names in your main yaml file.

这意味着您application-test.yml的资源目录中应该有文件。考虑到活动配置文件将覆盖主 yaml 文件中具有相同名称的属性。

For more information visit: http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-profiles.html

更多信息请访问:http: //docs.spring.io/spring-boot/docs/current/reference/html/boot-features-profiles.html

回答by Maksim Kostromin

  1. remove @PropertySourceannotation, you not need it
  2. rename your scheduling.ymlinto src/main/resources/application-scheduling.yml
  3. add in src/main/resources/application.ymlfile next line:

    spring.profiles.include: 'scheduling'

  1. 删除@PropertySource注释,你不需要它
  2. 将您的重命名scheduling.ymlsrc/main/resources/application-scheduling.yml
  3. src/main/resources/application.yml文件下一行添加:

    spring.profiles.include: 'scheduling'

回答by Maksim Kostromin

if I have a lot of configurations and/or environments, usually I do so:

如果我有很多配置和/或环境,通常我会这样做:

$ cat src/main/resources/application.yml:
spring:
  profiles:
    include: >
      profile1,
      profile2,
      ...
      profileN

$ ls -lah src/main/resources/config:
drwxr-xr-x  4 mak  staff   136B Apr 16 23:58 .
drwxr-xr-x  6 mak  staff   204B Apr 17 01:54 ..
-rw-r--r--  1 mak  staff    60B Apr 16 23:58 application-profile1.yml
-rw-r--r--  1 mak  staff    62B Apr 16 23:16 application-profile2.yml
...
-rw-r--r--  1 mak  staff    50B Apr 16 23:16 application-profileN.yml

回答by Deva

Suppose your application need 4 .yml files.

假设您的应用程序需要 4 个 .yml 文件。

application.yml
application-dev.yml
application-uat.yml
application-prod.yml

and you have to set different setting for each file.

并且您必须为每个文件设置不同的设置。

You just need to set your setting on appropriate environment such as dev, uat ot prod level and the have to add just one property in application.ymlfile.

您只需要在适当的环境中设置您的设置,例如 dev、uat ot prod 级别,并且只需在application.yml文件中添加一个属性。

  spring:  
    profiles:
       active: dev
    application: /* optional */
       name: Application Name