java Spring Boot - 加载多个 YAML 文件

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

Spring Boot - Load multiple YAML files

javaspringweb-servicesrestspring-boot

提问by Arun

I'm using Spring boot for my project and trying to load yaml files so that I can use the data in the files in my project. For example I have content in my application.yml like below.

我正在为我的项目使用 Spring boot 并尝试加载 yaml 文件,以便我可以使用项目中文件中的数据。例如,我的 application.yml 中有如下内容。

currency:
     code:
        840: 2
        484: 2
        999: 0

And in my code for reading the content from application.yml I have a class like this.

在我从 application.yml 读取内容的代码中,我有一个这样的类。

import java.util.Map;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConfigurationProperties(prefix = "currency")
public class Currency {

   private Map<String, String> code;

   public Map<String, String> getCode() {
       return code;
   }
   public void setCode(Map<String, String> code) {
       this.code = code;
   }
}

And If I print it in the test class

如果我在测试类中打印它

public class Test{

@Autowired
Currency currency;

Map<String, String> test = currency.getCode();
       for (Map.Entry<String, String> entry : test.entrySet()) {
           System.out.println("Key : " + entry.getKey() + " Value : " + entry.getValue());
       }
}

I'm getting like below which is perfect.

我越来越像下面这是完美的。

Key : 840 Value : 2
Key : 484 Value : 2
Key : 999 Value : 0

This is working if I keep the application.yml in my jar itself or I can read it by placing it in git repo aswell.

如果我将 application.yml 保存在我的 jar 中,或者我可以通过将它放在 git repo 中来读取它,这将起作用。

I tried keeping the content in currency.yml and in my application.yml I tried to use spring.config.location, so that I can read the content from currency.yml directly but it didn't work.

我尝试将内容保留在currency.yml 中,而在我的application.yml 中,我尝试使用spring.config.location,这样我就可以直接从currency.yml 中读取内容,但是没有用。

I would like to load files like currency.yml and codes.yml etc... which are custom yml files so that I can read multiple files content and use in my application. Are there any annotations or some approach I can use to load custom.yml files?

我想加载诸如currency.yml 和codes.yml 等文件……它们是自定义yml 文件,以便我可以读取多个文件内容并在我的应用程序中使用。我可以使用任何注释或某种方法来加载 custom.yml 文件吗?

回答by Arun

In the .yml file of the project add below content.

在项目的 .yml 文件中添加以下内容。

spring:
  profiles:
    include:
      - currency

Note:You can refer to multiple .yml files this way if you want. You need additional .yml files and Config classes like below example.

注意:如果需要,您可以通过这种方式引用多个 .yml 文件。您需要额外的 .yml 文件和配置类,如下例所示。

You need to have another .yml file which is application-currency.yml

您需要有另一个 .yml 文件,即application-currency.yml

In application-currency.yml you can add currencies, like below

在 application-currency.yml 中,您可以添加货币,如下所示

currencies:
  mappings:
    USD:
      fraction: 2
      symbol: $
      text: "US Dollar"
    MXN:
      fraction: 2
      symbol: $
      text: "Mexican Peso"

Your Configuration class would look like below.

您的 Configuration 类将如下所示。

package com.configuration;

import com.Currency;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
@EnableConfigurationProperties
@ConfigurationProperties(prefix = "currencies")
public class CurrencyConfiguration {

    private Map<String, Currency> mappings;

    public Map<String, Currency> getMappings() {
        return mappings;
    }

    public void setMappings(Map<String, Currency> mappings) {
        this.mappings = mappings;
    }
}

Wherever you need to use the currency details you can get by calling as shown below.

无论您在哪里需要使用货币详细信息,您都可以通过如下所示的电话获得。

@Autowired
private CurrencyConfiguration currencyConfiguration;

String currencyCodeAlpha3 = "USD";
Currency currency = currencyConfiguration.getMappings().get(currencyCodeAlpha3);

回答by Magnus

You can use spring.config.locationto specify paths to additional config files as a comma separated list.

您可以使用spring.config.location以逗号分隔的列表形式指定其他配置文件的路径。

java -jar myproject.jar --spring.config.location=classpath:/default.properties,classpath:/override.properties