Skip to content

Instantly share code, notes, and snippets.

@jamestrandung
Created May 9, 2019 09:28
Show Gist options
  • Save jamestrandung/df35410f335f30f94072c4624a34526c to your computer and use it in GitHub Desktop.
Save jamestrandung/df35410f335f30f94072c4624a34526c to your computer and use it in GitHub Desktop.
Simple implementation of YamlPropertySourceFactory
import java.io.IOException;
import java.util.Properties;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.core.io.support.PropertySourceFactory;
import org.springframework.lang.Nullable;
/**
* @author James Tran
*
*/
public class YamlPropertySourceFactory implements PropertySourceFactory {
@Override
public PropertySource<?> createPropertySource(@Nullable String name, EncodedResource resource) throws IOException {
Properties loadedProperties = this.loadYamlIntoProperties(resource.getResource());
return new PropertiesPropertySource((StringUtils.isNotBlank(name)) ? name : resource.getResource().getFilename(), loadedProperties);
}
private Properties loadYamlIntoProperties(Resource resource) {
YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
factory.setResources(resource);
factory.afterPropertiesSet();
return factory.getObject();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment