From 87b0a0df97a7df772c0f1cbd85fc3571515c76bd Mon Sep 17 00:00:00 2001 From: Looly Date: Mon, 6 Sep 2021 09:30:17 +0800 Subject: [PATCH] change setting --- CHANGELOG.md | 1 + .../main/java/cn/hutool/setting/Setting.java | 36 +++++++++---------- .../java/cn/hutool/setting/dialect/Props.java | 17 ++++----- 3 files changed, 26 insertions(+), 28 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 632cca5ee..c77276f9d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ * 【extra 】 SpringUtil增加getApplicationName、publishEvent方法(issue#I485NZ@Gitee) * 【core 】 BeanUtil.getProperty增加判空(issue#I488HA@Gitee) * 【core 】 OptionalBean弃用(pr#1182@Github) +* 【setting】 Setting、Props持有URL改为持有Resource(pr#1182@Github) ### 🐞Bug修复 diff --git a/hutool-setting/src/main/java/cn/hutool/setting/Setting.java b/hutool-setting/src/main/java/cn/hutool/setting/Setting.java index 2544653da..8cdd797d5 100644 --- a/hutool-setting/src/main/java/cn/hutool/setting/Setting.java +++ b/hutool-setting/src/main/java/cn/hutool/setting/Setting.java @@ -81,9 +81,9 @@ public class Setting extends AbsSetting implements Map { */ protected boolean isUseVariable; /** - * 设定文件的URL + * 设定文件的资源 */ - protected URL settingUrl; + protected Resource resource; private SettingLoader settingLoader; private WatchMonitor watchMonitor; @@ -187,10 +187,8 @@ public class Setting extends AbsSetting implements Map { * @return 成功初始化与否 */ public boolean init(Resource resource, Charset charset, boolean isUseVariable) { - if (resource == null) { - throw new NullPointerException("Null setting resource define!"); - } - this.settingUrl = resource.getUrl(); + Assert.notNull(resource, "Setting resource must be not null!"); + this.resource = resource; this.charset = charset; this.isUseVariable = isUseVariable; @@ -206,7 +204,7 @@ public class Setting extends AbsSetting implements Map { if (null == this.settingLoader) { settingLoader = new SettingLoader(this.groupedMap, this.charset, this.isUseVariable); } - return settingLoader.load(new UrlResource(this.settingUrl)); + return settingLoader.load(this.resource); } /** @@ -226,12 +224,12 @@ public class Setting extends AbsSetting implements Map { */ public void autoLoad(boolean autoReload, Consumer callback) { if (autoReload) { - Assert.notNull(this.settingUrl, "Setting URL is null !"); + Assert.notNull(this.resource, "Setting resource must be not null !"); if (null != this.watchMonitor) { // 先关闭之前的监听 this.watchMonitor.close(); } - this.watchMonitor = WatchUtil.createModify(this.settingUrl, new SimpleWatcher() { + this.watchMonitor = WatchUtil.createModify(resource.getUrl(), new SimpleWatcher() { @Override public void onModify(WatchEvent event, Path currentPath) { boolean success = load(); @@ -242,7 +240,7 @@ public class Setting extends AbsSetting implements Map { } }); this.watchMonitor.start(); - StaticLog.debug("Auto load for [{}] listenning...", this.settingUrl); + StaticLog.debug("Auto load for [{}] listenning...", this.resource.getUrl()); } else { IoUtil.close(this.watchMonitor); this.watchMonitor = null; @@ -256,7 +254,7 @@ public class Setting extends AbsSetting implements Map { * @since 5.4.3 */ public URL getSettingUrl() { - return this.settingUrl; + return (null == this.resource) ? null : this.resource.getUrl(); } /** @@ -265,7 +263,8 @@ public class Setting extends AbsSetting implements Map { * @return 获得设定文件的路径 */ public String getSettingPath() { - return (null == this.settingUrl) ? null : this.settingUrl.getPath(); + final URL settingUrl = getSettingUrl(); + return (null == settingUrl) ? null : settingUrl.getPath(); } /** @@ -376,8 +375,9 @@ public class Setting extends AbsSetting implements Map { * @since 5.4.3 */ public void store() { - Assert.notNull(this.settingUrl, "Setting path must be not null !"); - store(FileUtil.file(this.settingUrl)); + final URL resourceUrl = getSettingUrl(); + Assert.notNull(resourceUrl, "Setting path must be not null !"); + store(FileUtil.file(resourceUrl)); } /** @@ -751,7 +751,7 @@ public class Setting extends AbsSetting implements Map { result = prime * result + ((charset == null) ? 0 : charset.hashCode()); result = prime * result + groupedMap.hashCode(); result = prime * result + (isUseVariable ? 1231 : 1237); - result = prime * result + ((settingUrl == null) ? 0 : settingUrl.hashCode()); + result = prime * result + ((this.resource == null) ? 0 : this.resource.hashCode()); return result; } @@ -780,10 +780,10 @@ public class Setting extends AbsSetting implements Map { if (isUseVariable != other.isUseVariable) { return false; } - if (settingUrl == null) { - return other.settingUrl == null; + if (this.resource == null) { + return other.resource == null; } else { - return settingUrl.equals(other.settingUrl); + return resource.equals(other.resource); } } diff --git a/hutool-setting/src/main/java/cn/hutool/setting/dialect/Props.java b/hutool-setting/src/main/java/cn/hutool/setting/dialect/Props.java index 17ff59e81..6db9b9237 100644 --- a/hutool-setting/src/main/java/cn/hutool/setting/dialect/Props.java +++ b/hutool-setting/src/main/java/cn/hutool/setting/dialect/Props.java @@ -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.SettingRuntimeException; import java.io.BufferedReader; import java.io.File; @@ -62,9 +61,9 @@ public final class Props extends Properties implements BasicTypeGetter, // ----------------------------------------------------------------------- 私有属性 start /** - * 属性文件的URL + * 属性文件的Resource */ - private URL propertiesFileUrl; + private Resource resource; private WatchMonitor watchMonitor; /** * properties文件编码
@@ -275,10 +274,8 @@ public final class Props extends Properties implements BasicTypeGetter, * @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: [{}]", resource); - } + Assert.notNull(resource, "Props resource must be not null!"); + this.resource = resource; try (final BufferedReader reader = resource.getReader(charset)) { super.load(reader); @@ -291,7 +288,7 @@ public final class Props extends Properties implements BasicTypeGetter, * 重新加载配置文件 */ public void load() { - this.load(this.propertiesFileUrl); + this.load(this.resource); } /** @@ -301,12 +298,12 @@ public final class Props extends Properties implements BasicTypeGetter, */ public void autoLoad(boolean autoReload) { if (autoReload) { - Assert.notNull(this.propertiesFileUrl, "Properties URL is null !"); + Assert.notNull(this.resource, "Properties resource must be not null!"); if (null != this.watchMonitor) { // 先关闭之前的监听 this.watchMonitor.close(); } - this.watchMonitor = WatchUtil.createModify(this.propertiesFileUrl, new SimpleWatcher() { + this.watchMonitor = WatchUtil.createModify(this.resource.getUrl(), new SimpleWatcher() { @Override public void onModify(WatchEvent event, Path currentPath) { load();