2016-06-22 93 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

@阿倫你嘗試過嗎? –