更新时间:2022-12-18 10:59:45
在项目的.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);