兼容 spring cloud config

This commit is contained in:
thinkgem
2020-10-26 14:17:15 +08:00
parent 2747e0d5d3
commit c54a21cfd6

View File

@@ -4,25 +4,30 @@
package com.jeesite.common.io; package com.jeesite.common.io;
import java.io.IOException; import java.io.IOException;
import java.util.Collections; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Properties; import java.util.Properties;
import org.springframework.boot.env.OriginTrackedMapPropertySource; import org.springframework.boot.env.OriginTrackedMapPropertySource;
import org.springframework.boot.env.PropertiesPropertySourceLoader;
import org.springframework.boot.env.YamlPropertySourceLoader;
import org.springframework.core.Ordered; import org.springframework.core.Ordered;
import org.springframework.core.env.PropertySource; import org.springframework.core.env.PropertySource;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
import com.alibaba.fastjson.parser.ParserConfig; import com.alibaba.fastjson.parser.ParserConfig;
import com.jeesite.common.lang.StringUtils;
/** /**
* 配置文件加载Boot * 配置文件加载Boot
* @author ThinkGem * @author ThinkGem
* @version 2018-10-16 * @version 2020-10-26
*/ */
public class PropertyLoader implements org.springframework.boot.env.PropertySourceLoader, Ordered{ public class PropertyLoader extends PropertiesPropertySourceLoader implements org.springframework.boot.env.PropertySourceLoader, Ordered{
private static boolean isLoadPropertySource = false; private static boolean isLoadJeeSitePropertySource = false;
private PropertiesPropertySourceLoader propertiesPropertySourceLoader = new PropertiesPropertySourceLoader();
private YamlPropertySourceLoader yamlPropertySourceLoader = new YamlPropertySourceLoader();
@Override @Override
public String[] getFileExtensions() { public String[] getFileExtensions() {
@@ -31,13 +36,21 @@ public class PropertyLoader implements org.springframework.boot.env.PropertySour
@Override @Override
public List<PropertySource<?>> load(String name, Resource resource) throws IOException { public List<PropertySource<?>> load(String name, Resource resource) throws IOException {
if (!isLoadPropertySource){ List<PropertySource<?>> propertySources = new ArrayList<>();
isLoadPropertySource = true; if (!isLoadJeeSitePropertySource) {
isLoadJeeSitePropertySource = true;
ParserConfig.getGlobalInstance().setSafeMode(true); // 开启 FastJSON 安全模式 ParserConfig.getGlobalInstance().setSafeMode(true); // 开启 FastJSON 安全模式
Properties properties = PropertiesUtils.getInstance().getProperties(); Properties properties = PropertiesUtils.getInstance().getProperties();
return Collections.singletonList(new OriginTrackedMapPropertySource("jeesite", properties)); propertySources.add(new OriginTrackedMapPropertySource("jeesite", properties));
} else {
String ext = FileUtils.getFileExtension(resource.getFilename());
if (StringUtils.inString(ext, propertiesPropertySourceLoader.getFileExtensions())) {
propertySources.addAll(propertiesPropertySourceLoader.load(name, resource));
}else if (StringUtils.inString(ext, yamlPropertySourceLoader.getFileExtensions())) {
propertySources.addAll(yamlPropertySourceLoader.load(name, resource));
}
} }
return Collections.emptyList(); return propertySources;
} }
@Override @Override