Created
May 9, 2019 09:28
-
-
Save jamestrandung/df35410f335f30f94072c4624a34526c to your computer and use it in GitHub Desktop.
Simple implementation of YamlPropertySourceFactory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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