spring bootでyaml設定ファイルを読み込むサンプルコード
サンプルコード
public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
ResourceLoader loader = new DefaultResourceLoader();
YamlPropertySourceLoader yamlLoader = new YamlPropertySourceLoader();
List<String> yamlFilePaths = new ArrayList<>();
while(true){
String yamlFilePath = environment.getProperty(“load.yaml[“+i+"]");
if(yamlFilePath==null){
break;
}
i++;
if(“".equals(yamlFilePath)){
continue;
}
yamlFilePaths.add(yamlFilePath);
}
yamlFilePaths.forEach(filePath->{
try {
environment.getPropertySources().addLast(yamlLoader.load(filePath,loader.getResource(filePath),null));
} catch (IOException e) {
logger.error(“load property file failed!file:" + filePath);
throw new RuntimeException(e);
}
});
}