自分のプロジェクトに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ファイルを読み込むために使用できる注釈やいくつかのアプローチはありますか?
あなたはhttp://stackoverflow.com/questions/28303758/how-to-use-yamlpropertiesfactorybean-to-load-yaml-files-using-spring-framework-4に見ていましたか?ここではyamlファイルが単純なPropertyPlaceholderConfigurerとその特定の設定によってロードされています。 – tkachuko
@tkachukoいいえ、それを試みます。ありがとう。 – Arun
@Arunあなたはそれを試しましたか? –