From 7f2f1340686708edaa89b9f623d30f3e308bc35a Mon Sep 17 00:00:00 2001 From: Looly Date: Mon, 24 Feb 2025 10:25:06 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D`SettingLoader`load=E6=9C=AA?= =?UTF-8?q?=E6=8A=9B=E5=87=BA=E5=BC=82=E5=B8=B8=E5=AF=BC=E8=87=B4=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E6=96=87=E4=BB=B6=E6=97=A0=E6=B3=95=E6=AD=A3=E5=B8=B8?= =?UTF-8?q?=E9=81=8D=E5=8E=86=E7=9A=84=E9=97=AE=E9=A2=98=EF=BC=88pr#3868@G?= =?UTF-8?q?ithub=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dromara/hutool/core/io/file/FileUtil.java | 4 ++-- .../dromara/hutool/setting/SettingLoader.java | 22 +++++++++++-------- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/hutool-core/src/main/java/org/dromara/hutool/core/io/file/FileUtil.java b/hutool-core/src/main/java/org/dromara/hutool/core/io/file/FileUtil.java index 957efe21f..1914ea0f1 100644 --- a/hutool-core/src/main/java/org/dromara/hutool/core/io/file/FileUtil.java +++ b/hutool-core/src/main/java/org/dromara/hutool/core/io/file/FileUtil.java @@ -1240,11 +1240,11 @@ public class FileUtil { if (null == classPath) { // throw new NullPointerException("ClassPath is null !"); // 在jar运行模式中,ClassPath有可能获取不到,此时返回原始相对路径(此时获取的文件为相对工作目录) - return path; + return normalPath; } // 资源不存在的情况下使用标准化路径有问题,使用原始路径拼接后标准化路径 - return FileNameUtil.normalize(classPath.concat(Objects.requireNonNull(path))); + return FileNameUtil.normalize(classPath.concat(Objects.requireNonNull(normalPath))); } /** diff --git a/hutool-setting/src/main/java/org/dromara/hutool/setting/SettingLoader.java b/hutool-setting/src/main/java/org/dromara/hutool/setting/SettingLoader.java index 47844c91b..41e31b825 100644 --- a/hutool-setting/src/main/java/org/dromara/hutool/setting/SettingLoader.java +++ b/hutool-setting/src/main/java/org/dromara/hutool/setting/SettingLoader.java @@ -19,6 +19,7 @@ package org.dromara.hutool.setting; import org.dromara.hutool.core.io.IoUtil; import org.dromara.hutool.core.io.LineReader; import org.dromara.hutool.core.io.file.FileUtil; +import org.dromara.hutool.core.io.resource.NoResourceException; import org.dromara.hutool.core.io.resource.Resource; import org.dromara.hutool.core.lang.Assert; import org.dromara.hutool.core.regex.ReUtil; @@ -132,23 +133,26 @@ public class SettingLoader { * * @param resource 配置文件URL * @return 加载是否成功 + * @throws NoResourceException 如果资源不存在,抛出此异常 */ - public GroupedMap load(final Resource resource) { - if (resource == null) { - throw new NullPointerException("Null setting url define!"); - } - log.debug("Load setting file [{}]", resource); + public GroupedMap load(final Resource resource) throws NoResourceException{ + Assert.notNull(resource, "Null setting url define!"); + + GroupedMap groupedMap; InputStream settingStream = null; try { settingStream = resource.getStream(); - return load(settingStream); + groupedMap = load(settingStream); + log.debug("Load setting file [{}]", resource); } catch (final Exception e) { - log.error(e, "Load setting error!"); - // 加载错误跳过,返回空的map - return new GroupedMap(); + if(e instanceof NoResourceException){ + throw (NoResourceException)e; + } + throw new NoResourceException(e); } finally { IoUtil.closeQuietly(settingStream); } + return groupedMap; } /**