Spring Boot-加载多个YAML文件 [英] Spring Boot - Load multiple YAML files
问题描述
我正在为项目使用Spring Boot,并尝试加载yaml文件,以便可以在项目中使用文件中的数据.例如,我的application.yml中包含如下内容.
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.
currency:
code:
840: 2
484: 2
999: 0
在我从application.yml中读取内容的代码中,我有一个类似的类.
And in my code for reading the content from application.yml I have a class like this.
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());
}
}
我在下面变得很完美.
Key : 840 Value : 2
Key : 484 Value : 2
Key : 999 Value : 0
如果我将application.yml保留在jar本身中,或者也可以将其放置在git repo中进行读取,则此方法可以正常工作.
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.
我尝试将内容保留在currency.yml和application.yml中,我尝试使用spring.config.location,以便可以直接从currency.yml读取内容,但是没有用.
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和codes.yml等文件,这些文件是自定义的yml文件,以便我可以读取多个文件的内容并在我的应用程序中使用.我可以使用任何注释或某种方法来加载custom.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?
推荐答案
在项目的.yml文件中添加以下内容.
In the .yml file of the project add below content.
spring:
profiles:
include:
- currency
注意:如果需要,您可以通过这种方式引用多个.yml文件.您需要其他.yml文件和Config类,例如下面的示例.
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文件,该文件是 application-currency.yml
You need to have another .yml file which is application-currency.yml
在application-currency.yml中,您可以添加如下所示的货币
In application-currency.yml you can add currencies, like below
currencies:
mappings:
USD:
fraction: 2
symbol: $
text: "US Dollar"
MXN:
fraction: 2
symbol: $
text: "Mexican Peso"
您的配置类如下所示.
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);
这篇关于Spring Boot-加载多个YAML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!