config.YamlFileApplicationContextInitializer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of rocketmix-spring-boot-starter Show documentation
Show all versions of rocketmix-spring-boot-starter Show documentation
Shared configuration and tools. This project contains a custom spring boot module to facilitate new api project developpement startup process.
package config;
import java.io.IOException;
import java.util.List;
import org.springframework.boot.env.YamlPropertySourceLoader;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.Resource;
class YamlFileApplicationContextInitializer implements ApplicationContextInitializer {
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
addYamlPropertiesToSpringEnvironment(applicationContext);
}
private void addYamlPropertiesToSpringEnvironment(ConfigurableApplicationContext applicationContext) {
try {
Resource resource = applicationContext.getResource("classpath:default-application.yml");
YamlPropertySourceLoader yamlPropertySourceLoader = new YamlPropertySourceLoader();
List> yamlProperties = yamlPropertySourceLoader.load("default", resource);
yamlProperties.forEach(p -> applicationContext.getEnvironment().getPropertySources().addLast(p));
} catch (IOException e) {
throw new RuntimeException("Unable to load resource file", e);
}
}
}