GlobalDbSetting优化默认配置读取规则,优先读取文件而非jar中的文件(issue#900@Github)

This commit is contained in:
Looly 2025-01-12 17:52:42 +08:00
parent 5f57da864d
commit b116be492a
3 changed files with 27 additions and 11 deletions

View File

@ -2,11 +2,13 @@
# 🚀Changelog
-------------------------------------------------------------------------------------------------------------
# 5.8.36(2025-01-10)
# 5.8.36(2025-01-12)
### 🐣新特性
* 【crypto 】 增加BCUtil.decodeECPrivateKey方法issue#3829@Github
* 【core 】 增加HtmlUtil.cleanEmptyTag方法pr#3838@Github
* 【db 】 GlobalDbSetting优化默认配置读取规则优先读取文件而非jar中的文件issue#900@Github
### 🐞Bug修复
* 【aop 】 修复ProxyUtil可能的空指针问题issue#IBF20Z@Gitee
* 【core 】 修复XmlUtil转义调用方法错误问题修复XmlEscape未转义单引号问题pr#3837@Github

View File

@ -79,6 +79,9 @@ public class FileResource implements Resource, Serializable {
@Override
public InputStream getStream() throws NoResourceException {
if (!this.file.exists()) {
throw new NoResourceException("File [{}] not exist!", this.file.getAbsolutePath());
}
return FileUtil.getInputStream(this.file);
}

View File

@ -1,6 +1,7 @@
package cn.hutool.db;
import cn.hutool.core.io.resource.NoResourceException;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.db.sql.SqlLog;
import cn.hutool.log.level.Level;
import cn.hutool.setting.Setting;
@ -83,20 +84,30 @@ public class GlobalDbConfig {
throw new NoResourceException("Customize db setting file [{}] not found !", dbSettingPath);
}
} else {
try {
setting = new Setting(DEFAULT_DB_SETTING_PATH, true);
} catch (NoResourceException e) {
// 尝试ClassPath下直接读取配置文件
try {
setting = new Setting(DEFAULT_DB_SETTING_PATH2, true);
} catch (NoResourceException e2) {
throw new NoResourceException("Default db setting [{}] or [{}] in classpath not found !", DEFAULT_DB_SETTING_PATH, DEFAULT_DB_SETTING_PATH2);
}
}
setting = tryDefaultDbSetting();
}
return setting;
}
/**
* 获取自定义或默认位置数据库配置{@link Setting}
*
* @return 数据库配置
* @since 5.8.36
*/
private static Setting tryDefaultDbSetting() {
final String[] defaultDbSettingPaths = {"file:" + DEFAULT_DB_SETTING_PATH, "file:" + DEFAULT_DB_SETTING_PATH2, DEFAULT_DB_SETTING_PATH, DEFAULT_DB_SETTING_PATH2};
for (final String settingPath : defaultDbSettingPaths) {
try {
return new Setting(settingPath, true);
} catch (final NoResourceException e) {
// ignore
}
}
throw new NoResourceException("Default db settings [{}] in classpath not found !", ArrayUtil.join(defaultDbSettingPaths, ","));
}
/**
* 设置全局配置是否通过debug日志显示SQL
*