2016-06-22 9 views
1

自分のプロジェクトにSpringブートを使用していて、yamlファイルをロードしようとしているので、プロジェクトのファイルにデータを使用できます。例えば私は以下のように私のapplication.ymlに内容を持っています。Springブート - 複数のYAMLファイルをロード

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 

私は私のjarファイル自体にapplication.ymlを維持するか、私はGitのレポaswellに置くことで、それを読むことができる場合、これは働いています。

私はcurrency.ymlの内容を保持しようとしましたが、application.ymlでspring.config.locationを使用しようとしました。そのため、currency.ymlから直接コンテンツを読むことができますが、動作しませんでした。

currency.ymlやcodes.ymlなどのファイルをロードしたいのですが...カスタム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

@Arunあなたはそれを試しましたか? –

答えて

2

spring.config.locationを使用すると、追加の設定ファイルへのパスをコンマ区切りのリストとして指定できます。

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

一度試してみると更新されます。 – Arun

+0

@Arun試しましたか? –

関連する問題