refactor: 封装公共配置,降低使用复杂度

This commit is contained in:
2023-11-25 20:45:16 +08:00
parent 4a8e4e4408
commit dafb2d8f5b
9 changed files with 123 additions and 0 deletions

View File

@@ -0,0 +1,46 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
* <p>
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.gnu.org/licenses/lgpl.html
* <p>
* 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.
*/
package top.charles7c.continew.starter.core.handler;
import cn.hutool.core.util.StrUtil;
import org.springframework.boot.env.YamlPropertySourceLoader;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.DefaultPropertySourceFactory;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.lang.Nullable;
import java.io.IOException;
/**
* 通用配置文件读取工厂DefaultPropertySourceFactory 仅支持 properties 配置文件读取,详见:<a href="https://docs.spring.io/spring-boot/docs/2.0.6.RELEASE/reference/html/boot-features-external-config.html#boot-features-external-config-yaml-shortcomings">YAML Shortcomings</a>
*
* @author Charles7c
* @since 1.0.0
*/
public class GeneralPropertySourceFactory extends DefaultPropertySourceFactory {
@Override
public PropertySource<?> createPropertySource(@Nullable String name, EncodedResource encodedResource) throws IOException {
Resource resource = encodedResource.getResource();
String resourceName = resource.getFilename();
if (StrUtil.isNotBlank(resourceName) && StrUtil.endWithAny(resourceName, ".yml", ".yaml")) {
return new YamlPropertySourceLoader().load(resourceName, resource).get(0);
}
return super.createPropertySource(name, encodedResource);
}
}