|
|
|
|
@@ -5,19 +5,23 @@ import java.util.Properties;
|
|
|
|
|
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;
|
|
|
|
|
import org.springframework.util.StringUtils;
|
|
|
|
|
|
|
|
|
|
public class YamlPropertySourceFactory implements PropertySourceFactory {
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public PropertySource<?> createPropertySource(String name, EncodedResource encodedResource) {
|
|
|
|
|
Resource resource = encodedResource.getResource();
|
|
|
|
|
YamlPropertiesFactoryBean factoryBean = new YamlPropertiesFactoryBean();
|
|
|
|
|
factoryBean.setResources(resource);
|
|
|
|
|
public PropertySource<?> createPropertySource(@Nullable String name, EncodedResource resource) {
|
|
|
|
|
Properties yamlProperties = loadYamlProperties(resource);
|
|
|
|
|
String sourceName = StringUtils.hasText(name) ? name : resource.getResource().getFilename();
|
|
|
|
|
return new PropertiesPropertySource(Objects.requireNonNull(sourceName), Objects.requireNonNull(yamlProperties));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Properties properties = factoryBean.getObject();
|
|
|
|
|
return new PropertiesPropertySource(Objects.requireNonNull(resource.getFilename()), Objects.requireNonNull(properties));
|
|
|
|
|
private Properties loadYamlProperties(EncodedResource resource) {
|
|
|
|
|
YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
|
|
|
|
|
factory.setResources(resource.getResource());
|
|
|
|
|
return factory.getObject();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|