mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
fix code
This commit is contained in:
@@ -2,7 +2,7 @@ package cn.hutool.setting;
|
||||
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
import cn.hutool.core.io.resource.UrlResource;
|
||||
import cn.hutool.core.io.resource.Resource;
|
||||
import cn.hutool.core.lang.Assert;
|
||||
import cn.hutool.core.util.CharUtil;
|
||||
import cn.hutool.core.util.CharsetUtil;
|
||||
@@ -70,17 +70,17 @@ public class SettingLoader {
|
||||
/**
|
||||
* 加载设置文件
|
||||
*
|
||||
* @param urlResource 配置文件URL
|
||||
* @param resource 配置文件URL
|
||||
* @return 加载是否成功
|
||||
*/
|
||||
public boolean load(UrlResource urlResource) {
|
||||
if (urlResource == null) {
|
||||
public boolean load(Resource resource) {
|
||||
if (resource == null) {
|
||||
throw new NullPointerException("Null setting url define!");
|
||||
}
|
||||
log.debug("Load setting file [{}]", urlResource);
|
||||
log.debug("Load setting file [{}]", resource);
|
||||
InputStream settingStream = null;
|
||||
try {
|
||||
settingStream = urlResource.getStream();
|
||||
settingStream = resource.getStream();
|
||||
load(settingStream);
|
||||
} catch (Exception e) {
|
||||
log.error(e, "Load setting error!");
|
||||
|
@@ -1,6 +1,6 @@
|
||||
package cn.hutool.setting;
|
||||
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.core.io.file.FileNameUtil;
|
||||
import cn.hutool.core.io.resource.NoResourceException;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
|
||||
@@ -27,22 +27,13 @@ public class SettingUtil {
|
||||
* @return 当前环境下配置文件
|
||||
*/
|
||||
public static Setting get(String name) {
|
||||
Setting setting = SETTING_MAP.get(name);
|
||||
if (null == setting) {
|
||||
synchronized (SettingUtil.class) {
|
||||
setting = SETTING_MAP.get(name);
|
||||
if (null == setting) {
|
||||
String filePath = name;
|
||||
String extName = FileUtil.extName(filePath);
|
||||
if (StrUtil.isEmpty(extName)) {
|
||||
filePath = filePath + "." + Setting.EXT_NAME;
|
||||
}
|
||||
setting = new Setting(filePath, true);
|
||||
SETTING_MAP.put(name, setting);
|
||||
}
|
||||
return SETTING_MAP.computeIfAbsent(name, (filePath)->{
|
||||
final String extName = FileNameUtil.extName(filePath);
|
||||
if (StrUtil.isEmpty(extName)) {
|
||||
filePath = filePath + "." + Setting.EXT_NAME;
|
||||
}
|
||||
}
|
||||
return setting;
|
||||
return new Setting(filePath, true);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -21,7 +21,6 @@ import cn.hutool.core.util.CharsetUtil;
|
||||
import cn.hutool.core.util.ReflectUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.log.StaticLog;
|
||||
import cn.hutool.setting.Setting;
|
||||
import cn.hutool.setting.SettingRuntimeException;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
@@ -239,7 +238,7 @@ public final class Props extends Properties implements BasicTypeGetter<String>,
|
||||
if (null != charset) {
|
||||
this.charset = charset;
|
||||
}
|
||||
this.load(new UrlResource(propertiesUrl));
|
||||
this.load(propertiesUrl);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -257,16 +256,26 @@ public final class Props extends Properties implements BasicTypeGetter<String>,
|
||||
|
||||
/**
|
||||
* 初始化配置文件
|
||||
*
|
||||
* @param urlResource {@link UrlResource}
|
||||
*
|
||||
* @param url {@link URL}
|
||||
* @since 5.5.2
|
||||
*/
|
||||
public void load(Resource urlResource) {
|
||||
this.propertiesFileUrl = urlResource.getUrl();
|
||||
public void load(URL url) {
|
||||
load(new UrlResource(url));
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化配置文件
|
||||
*
|
||||
* @param resource {@link Resource}
|
||||
*/
|
||||
public void load(Resource resource) {
|
||||
this.propertiesFileUrl = resource.getUrl();
|
||||
if (null == this.propertiesFileUrl) {
|
||||
throw new SettingRuntimeException("Can not find properties file: [{}]", urlResource);
|
||||
throw new SettingRuntimeException("Can not find properties file: [{}]", resource);
|
||||
}
|
||||
|
||||
try (final BufferedReader reader = urlResource.getReader(charset)) {
|
||||
try (final BufferedReader reader = resource.getReader(charset)) {
|
||||
super.load(reader);
|
||||
} catch (IOException e) {
|
||||
throw new IORuntimeException(e);
|
||||
@@ -277,7 +286,7 @@ public final class Props extends Properties implements BasicTypeGetter<String>,
|
||||
* 重新加载配置文件
|
||||
*/
|
||||
public void load() {
|
||||
this.load(new UrlResource(this.propertiesFileUrl));
|
||||
this.load(this.propertiesFileUrl);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -20,7 +20,6 @@ public class PropsUtil {
|
||||
* 配置文件缓存
|
||||
*/
|
||||
private static final Map<String, Props> propsMap = new ConcurrentHashMap<>();
|
||||
private static final Object lock = new Object();
|
||||
|
||||
/**
|
||||
* 获取当前环境下的配置文件<br>
|
||||
@@ -30,22 +29,13 @@ public class PropsUtil {
|
||||
* @return 当前环境下配置文件
|
||||
*/
|
||||
public static Props get(String name) {
|
||||
Props props = propsMap.get(name);
|
||||
if (null == props) {
|
||||
synchronized (lock) {
|
||||
props = propsMap.get(name);
|
||||
if (null == props) {
|
||||
String filePath = name;
|
||||
String extName = FileUtil.extName(filePath);
|
||||
if (StrUtil.isEmpty(extName)) {
|
||||
filePath = filePath + "." + Props.EXT_NAME;
|
||||
}
|
||||
props = new Props(filePath);
|
||||
propsMap.put(name, props);
|
||||
}
|
||||
return propsMap.computeIfAbsent(name, (filePath)->{
|
||||
final String extName = FileUtil.extName(filePath);
|
||||
if (StrUtil.isEmpty(extName)) {
|
||||
filePath = filePath + "." + Props.EXT_NAME;
|
||||
}
|
||||
}
|
||||
return props;
|
||||
return new Props(filePath);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -66,4 +56,14 @@ public class PropsUtil {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取系统参数,例如用户在执行java命令时定义的 -Duse=hutool
|
||||
*
|
||||
* @return 系统参数Props
|
||||
* @since 5.5.2
|
||||
*/
|
||||
public static Props getSystemProps(){
|
||||
return new Props(System.getProperties());
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user