【发布时间】:2016-10-24 19:02:45
【问题描述】:
我正在为我的项目使用 Spring boot,并尝试加载 yaml 文件,以便我可以使用项目中文件中的数据。例如,我的 application.yml 中有如下内容。
currency:
code:
840: 2
484: 2
999: 0
在我从 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;
}
}
如果我在测试类中打印它
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 中来读取它,这将起作用。
我尝试将内容保存在currency.yml 中,在我的application.yml 中我尝试使用spring.config.location,以便我可以直接从currency.yml 中读取内容,但它不起作用。
我想加载像currency.yml 和codes.yml 等自定义yml 文件这样的文件,以便我可以读取多个文件内容并在我的应用程序中使用。是否有任何注释或某种方法可以用来加载 custom.yml 文件?
【问题讨论】:
-
你查看stackoverflow.com/questions/28303758/…了吗?这里 yaml 文件是通过简单的 PropertyPlaceholderConfigurer 及其特定配置加载的。
-
@tkachuko 不。会尝试的。谢谢。
-
@Arun 你试过了吗?
-
@AdrianIvan 嘿,不,我没有,但是当我有机会时,我需要这样做。
标签: java spring web-services rest spring-boot