All Downloads are FREE. Search and download functionalities are using the official Maven repository.

config.YamlFileApplicationContextInitializer Maven / Gradle / Ivy

Go to download

Shared configuration and tools. This project contains a custom spring boot module to facilitate new api project developpement startup process.

There is a newer version: 1.0.42
Show newest version
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);
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy