From c1dac72b29b935b70b0a8bce6dd8bd26f43df711 Mon Sep 17 00:00:00 2001 From: wentao Date: Sat, 13 Jan 2018 22:11:46 +0800 Subject: [PATCH 1/4] =?UTF-8?q?1=20=E6=96=B0=E5=A2=9E=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E8=AF=BB=E5=8F=96jar=E5=8C=85=E5=A4=96=E9=83=A8application.pro?= =?UTF-8?q?perties=E4=B8=BA=E7=BB=9D=E5=AF=B9=E8=B7=AF=E5=BE=84=202=20?= =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=94=AF=E6=8C=81application.properties?= =?UTF-8?q?=E9=85=8D=E7=BD=AEconfigDir=EF=BC=8C=E5=8F=AF=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E5=8A=A0=E8=BD=BD=E8=AF=A5=E7=9B=AE=E5=BD=95=E4=B8=8B=E6=89=80?= =?UTF-8?q?=E6=9C=89=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../impl/PropertiesConfigureLoader.java | 57 ++++++++++++++++++- 1 file changed, 56 insertions(+), 1 deletion(-) diff --git a/nutzboot-core/src/main/java/org/nutz/boot/config/impl/PropertiesConfigureLoader.java b/nutzboot-core/src/main/java/org/nutz/boot/config/impl/PropertiesConfigureLoader.java index 49e0065c..f1a8f091 100644 --- a/nutzboot-core/src/main/java/org/nutz/boot/config/impl/PropertiesConfigureLoader.java +++ b/nutzboot-core/src/main/java/org/nutz/boot/config/impl/PropertiesConfigureLoader.java @@ -3,6 +3,7 @@ package org.nutz.boot.config.impl; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; +import java.util.ArrayList; import org.nutz.ioc.impl.PropertiesProxy; import org.nutz.lang.Streams; @@ -16,6 +17,42 @@ import org.nutz.lang.Streams; */ public class PropertiesConfigureLoader extends AbstractConfigureLoader { + // 获取应用程序绝对路径 + private static String getBasePath() { + String basePath = PropertiesConfigureLoader.class.getProtectionDomain().getCodeSource().getLocation().getPath(); + int lastIndex = basePath.lastIndexOf(File.separator); + basePath = basePath.substring(0, lastIndex); + try { + basePath = java.net.URLDecoder.decode(basePath, "UTF-8"); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + } + return basePath; + } + + // 根据目录和文件名拼接绝对路径 + private static String getPath(String... name) { + String path = getBasePath(); + for(int i=0; i< name.length; i++) { + path = path + File.separator + name[i]; + } + return path; + } + + // 获取指定目录下所有指定扩展名的文件列表 + private static ArrayList getFiles(String path, String extName) { + ArrayList fileNames = new ArrayList<>(); + File file = new File(path); + if(file.isDirectory()) { + for(File f : file.listFiles()) { + if(f.getName().endsWith(extName)) { + fileNames.add(f.getName()); + } + } + } + return fileNames; + } + public void init() throws Exception { // 首先, 确定一些从什么路径加载配置文件,默认值application.properties String path = envHolder.get("nutz.boot.configure.properties_path", "application.properties"); @@ -28,8 +65,9 @@ public class PropertiesConfigureLoader extends AbstractConfigureLoader { } } // 如果当前文件夹存在application.properties,读取之 + String applicationPath = getPath("application.properties"); try { - File tmp = new File(path); + File tmp = new File(applicationPath); if (tmp.exists() && tmp.canRead()) { try (FileInputStream ins = new FileInputStream(tmp)) { conf.load(Streams.utf8r(ins), false); @@ -38,6 +76,23 @@ public class PropertiesConfigureLoader extends AbstractConfigureLoader { } catch (Throwable e) { } + // 如果conf内含有configDir配置,则读取configDir下的所有配置文件 + // 配置示例: configDir=config, 那么读取的就是jar包当前目录下config子目录下的所有properties文件 + if(conf.has("configDir")) { + String configDir = getPath(conf.get("configDir")); + for(String fileName: getFiles(configDir, "properties")) { + try { + File tmp = new File(getPath(fileName)); + if (tmp.exists() && tmp.canRead()) { + try (FileInputStream ins = new FileInputStream(tmp)) { + conf.load(Streams.utf8r(ins), false); + } + } + } + catch (Throwable e) { + } + } + } // 也许命令行里面指定了profile,需要提前load进来 PropertiesProxy tmp = new PropertiesProxy(); if (args != null) { From 6f83fc2268393e7dae2e61d730e4b71320cf8609 Mon Sep 17 00:00:00 2001 From: wentao Date: Sat, 13 Jan 2018 23:04:33 +0800 Subject: [PATCH 2/4] =?UTF-8?q?1=E3=80=81=E4=BF=AE=E6=AD=A3=E5=8A=A0?= =?UTF-8?q?=E8=BD=BD=E8=B7=AF=E5=BE=84=E4=BD=BF=E7=94=A8path=202=E3=80=81?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=8A=A8=E6=80=81=E8=AF=BB=E5=8F=96=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E9=A1=B9=E5=90=8D=E7=A7=B0=E4=B8=BA=EF=BC=9Anutz.boot?= =?UTF-8?q?.configure.properties.dir?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../nutz/boot/config/impl/PropertiesConfigureLoader.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/nutzboot-core/src/main/java/org/nutz/boot/config/impl/PropertiesConfigureLoader.java b/nutzboot-core/src/main/java/org/nutz/boot/config/impl/PropertiesConfigureLoader.java index f1a8f091..48045c48 100644 --- a/nutzboot-core/src/main/java/org/nutz/boot/config/impl/PropertiesConfigureLoader.java +++ b/nutzboot-core/src/main/java/org/nutz/boot/config/impl/PropertiesConfigureLoader.java @@ -65,9 +65,8 @@ public class PropertiesConfigureLoader extends AbstractConfigureLoader { } } // 如果当前文件夹存在application.properties,读取之 - String applicationPath = getPath("application.properties"); try { - File tmp = new File(applicationPath); + File tmp = new File(getPath(path)); if (tmp.exists() && tmp.canRead()) { try (FileInputStream ins = new FileInputStream(tmp)) { conf.load(Streams.utf8r(ins), false); @@ -78,8 +77,8 @@ public class PropertiesConfigureLoader extends AbstractConfigureLoader { } // 如果conf内含有configDir配置,则读取configDir下的所有配置文件 // 配置示例: configDir=config, 那么读取的就是jar包当前目录下config子目录下的所有properties文件 - if(conf.has("configDir")) { - String configDir = getPath(conf.get("configDir")); + if(conf.has("nutz.boot.configure.properties.dir")) { + String configDir = getPath(conf.get("nutz.boot.configure.properties.dir")); for(String fileName: getFiles(configDir, "properties")) { try { File tmp = new File(getPath(fileName)); From 130b1be742b432ba13f184462c16120b0820bada Mon Sep 17 00:00:00 2001 From: wentao Date: Sat, 13 Jan 2018 23:16:22 +0800 Subject: [PATCH 3/4] write README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index ae5f5a26..113928ae 100644 --- a/README.md +++ b/README.md @@ -109,6 +109,7 @@ public class MainLauncher { * [haoqoo](https://github.com/haoqoo)(提交velocity) * [鱼夫](https://gitee.com/yustory)(正在踩NB+U家三剑客的坑) * [幸福的旁边](https://github.com/happyday517)(提交caffeine方法缓存) +* [文涛](https://github.com/swtseaman) (新增支持加载外部配置文件) * 还有您的名字哦,告知我们吧 ## 采用NutzBoot的公司 From 36ec53b1aee1ccfb4599e172c513228f65a619ed Mon Sep 17 00:00:00 2001 From: wentao Date: Sat, 13 Jan 2018 23:22:05 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=9C=AA=E5=BC=95?= =?UTF-8?q?=E7=94=A8=20UnsupportedEncodingException=20=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/nutz/boot/config/impl/PropertiesConfigureLoader.java | 1 + 1 file changed, 1 insertion(+) diff --git a/nutzboot-core/src/main/java/org/nutz/boot/config/impl/PropertiesConfigureLoader.java b/nutzboot-core/src/main/java/org/nutz/boot/config/impl/PropertiesConfigureLoader.java index 48045c48..9aa720d0 100644 --- a/nutzboot-core/src/main/java/org/nutz/boot/config/impl/PropertiesConfigureLoader.java +++ b/nutzboot-core/src/main/java/org/nutz/boot/config/impl/PropertiesConfigureLoader.java @@ -4,6 +4,7 @@ import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.util.ArrayList; +import java.io.UnsupportedEncodingException; import org.nutz.ioc.impl.PropertiesProxy; import org.nutz.lang.Streams;