sviolet.slate.common.spring.property.YamlPropertySourceFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of slate-common Show documentation
Show all versions of slate-common Show documentation
Comprehensive Java library for spring framework (Java8+)
/*
* Copyright (C) 2015-2019 S.Violet
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Project GitHub: https://github.com/shepherdviolet/slate
* Email: [email protected]
*/
package sviolet.slate.common.spring.property;
import org.springframework.boot.env.YamlPropertySourceLoader;
import org.springframework.boot.origin.Origin;
import org.springframework.boot.origin.OriginLookup;
import org.springframework.core.env.EnumerablePropertySource;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.core.io.support.PropertySourceFactory;
import java.io.IOException;
import java.util.List;
/**
* [Spring Boot 专用] 使用PropertySource注解加载YAML配置文件
*
*
* @
PropertySource(factory = YamlPropertySourceFactory.class, value = {
* "properties/yaml1.yml",
* "properties/yaml2.yml",
* })
*
*
* @author S.Violet
*/
public class YamlPropertySourceFactory implements PropertySourceFactory {
/*
@PropertySource(factory = YamlPropertySourceFactory.class, value = {
"properties/yaml1.yml",
"properties/yaml2.yml",
})
*/
private static final YamlPropertySourceLoader LOADER = new YamlPropertySourceLoader();
protected static final EmptyPropertySource EMPTY_PROPERTY_SOURCE = new EmptyPropertySource();
@Override
public PropertySource> createPropertySource(String name, EncodedResource resource) throws IOException {
if (resource == null){
return EMPTY_PROPERTY_SOURCE;
}
List> propertySources = LOADER.load(resource.getResource().getFilename(), resource.getResource());
if (propertySources == null || propertySources.size() <= 0) {
return EMPTY_PROPERTY_SOURCE;
}
return propertySources.get(0);
}
/**
* 空配置源
*/
private static class EmptyPropertySource extends EnumerablePropertySource