From 913a77d01fd35e40c04d752bc47bc80a78b5fa65 Mon Sep 17 00:00:00 2001 From: thinkgem Date: Mon, 26 Oct 2020 14:17:15 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=BC=E5=AE=B9=20spring=20cloud=20config?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/jeesite/common/io/PropertyLoader.java | 29 ++++++++++++++----- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/common/src/main/java/com/jeesite/common/io/PropertyLoader.java b/common/src/main/java/com/jeesite/common/io/PropertyLoader.java index c757d0e8..8c5e5f0a 100644 --- a/common/src/main/java/com/jeesite/common/io/PropertyLoader.java +++ b/common/src/main/java/com/jeesite/common/io/PropertyLoader.java @@ -4,25 +4,30 @@ package com.jeesite.common.io; import java.io.IOException; -import java.util.Collections; +import java.util.ArrayList; import java.util.List; import java.util.Properties; 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.env.PropertySource; import org.springframework.core.io.Resource; import com.alibaba.fastjson.parser.ParserConfig; +import com.jeesite.common.lang.StringUtils; /** * 配置文件加载(Boot) * @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 public String[] getFileExtensions() { @@ -31,13 +36,21 @@ public class PropertyLoader implements org.springframework.boot.env.PropertySour @Override public List> load(String name, Resource resource) throws IOException { - if (!isLoadPropertySource){ - isLoadPropertySource = true; + List> propertySources = new ArrayList<>(); + if (!isLoadJeeSitePropertySource) { + isLoadJeeSitePropertySource = true; ParserConfig.getGlobalInstance().setSafeMode(true); // 开启 FastJSON 安全模式 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