update PropertiesUtils
This commit is contained in:
@@ -4,7 +4,6 @@
|
|||||||
*/
|
*/
|
||||||
package com.jeesite.common.io;
|
package com.jeesite.common.io;
|
||||||
|
|
||||||
import com.jeesite.common.codec.EncodeUtils;
|
|
||||||
import com.jeesite.common.collect.SetUtils;
|
import com.jeesite.common.collect.SetUtils;
|
||||||
import com.jeesite.common.lang.ObjectUtils;
|
import com.jeesite.common.lang.ObjectUtils;
|
||||||
import com.jeesite.common.lang.StringUtils;
|
import com.jeesite.common.lang.StringUtils;
|
||||||
@@ -17,6 +16,8 @@ import org.springframework.core.io.Resource;
|
|||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
@@ -62,9 +63,7 @@ public class PropertiesUtils {
|
|||||||
//configSet.add("classpath:config/jeesite.yml");
|
//configSet.add("classpath:config/jeesite.yml");
|
||||||
// 获取全局设置默认的配置文件(以下是支持环境配置的属性文件)
|
// 获取全局设置默认的配置文件(以下是支持环境配置的属性文件)
|
||||||
Set<String> set = SetUtils.newLinkedHashSet();
|
Set<String> set = SetUtils.newLinkedHashSet();
|
||||||
for (String configFile : DEFAULT_CONFIG_FILE){
|
set.addAll(Arrays.asList(DEFAULT_CONFIG_FILE));
|
||||||
set.add(configFile);
|
|
||||||
}
|
|
||||||
// 获取 spring.config.location 外部自定义的配置文件
|
// 获取 spring.config.location 外部自定义的配置文件
|
||||||
String customConfigs = System.getProperty("spring.config.location");
|
String customConfigs = System.getProperty("spring.config.location");
|
||||||
if (StringUtils.isNotBlank(customConfigs)){
|
if (StringUtils.isNotBlank(customConfigs)){
|
||||||
@@ -79,7 +78,7 @@ public class PropertiesUtils {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
// 获取 spring.profiles.active 活动环境名称的配置文件
|
// 获取 spring.profiles.active 活动环境名称的配置文件
|
||||||
String[] configFiles = set.toArray(new String[set.size()]);
|
String[] configFiles = set.toArray(new String[0]);
|
||||||
String profiles = System.getProperty("spring.profiles.active");
|
String profiles = System.getProperty("spring.profiles.active");
|
||||||
if (StringUtils.isBlank(profiles)){
|
if (StringUtils.isBlank(profiles)){
|
||||||
PropertiesUtils propsTemp = new PropertiesUtils(configFiles);
|
PropertiesUtils propsTemp = new PropertiesUtils(configFiles);
|
||||||
@@ -99,7 +98,7 @@ public class PropertiesUtils {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
configFiles = configSet.toArray(new String[configSet.size()]);
|
configFiles = configSet.toArray(new String[0]);
|
||||||
logger.debug("Trying: {}", (Object)configFiles);
|
logger.debug("Trying: {}", (Object)configFiles);
|
||||||
INSTANCE = new PropertiesUtils(configFiles);
|
INSTANCE = new PropertiesUtils(configFiles);
|
||||||
}
|
}
|
||||||
@@ -113,12 +112,11 @@ public class PropertiesUtils {
|
|||||||
Resource resource = ResourceUtils.getResource(location);
|
Resource resource = ResourceUtils.getResource(location);
|
||||||
if (resource.exists()){
|
if (resource.exists()){
|
||||||
if (location.endsWith(".properties")){
|
if (location.endsWith(".properties")){
|
||||||
try (InputStreamReader is = new InputStreamReader(resource.getInputStream(), EncodeUtils.UTF_8)){
|
try (InputStreamReader is = new InputStreamReader(resource.getInputStream(), StandardCharsets.UTF_8)){
|
||||||
properties.load(is);
|
properties.load(is);
|
||||||
configSet.add(location);
|
configSet.add(location);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
System.err.println("Load " + location + " failure.");
|
logger.error("Load {} failure.", location, e);
|
||||||
e.printStackTrace();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (location.endsWith(".yml")){
|
else if (location.endsWith(".yml")){
|
||||||
@@ -131,8 +129,7 @@ public class PropertiesUtils {
|
|||||||
}
|
}
|
||||||
configSet.add(location);
|
configSet.add(location);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
System.err.println("Load " + location + " failure.");
|
logger.error("Load {} failure.", location, e);
|
||||||
e.printStackTrace();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -314,7 +311,6 @@ public class PropertiesUtils {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 设置环境属性
|
* 设置环境属性
|
||||||
* @param environment
|
|
||||||
*/
|
*/
|
||||||
public static void setEnvironment(Environment environment) {
|
public static void setEnvironment(Environment environment) {
|
||||||
PropertiesUtils.environment = environment;
|
PropertiesUtils.environment = environment;
|
||||||
|
|||||||
Reference in New Issue
Block a user