2016-06-22 169 views
1

我使用Spring启动我的项目,并试图加载YAML文件,这样我可以使用这些文件中的数据在我的项目多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在我的罐子本身或者我可以将其放置在混帐回购藏汉读它这是工作。

我试着保留currency.yml中的内容,并在我的application.yml中尝试使用spring.config.location,这样我就可以直接从currency.yml读取内容,但它不起作用。

我想加载像currency.yml和codes.yml等文件......这是定制阳明文件,这样我可以读取多个文件的内容,并在我的应用程序中使用。是否有任何注释或一些我可以用来加载custom.yml文件的方法?

+1

你看看http://stackoverflow.com/questions/28303758/how-to-use-yamlpropertiesfactorybean-to-load-yaml-files-using-spring-framework-4?这里yaml文件正在通过简单的PropertyPlaceholderConfigurer及其特定配置进行加载。 – tkachuko

+0

@tkachuko号会尝试。谢谢。 – Arun

+0

@阿伦你尝试过吗? –

回答

2

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

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

谢谢。一旦我尝试了,会更新。 – Arun

+0

@阿伦你尝试过吗? –