mirror of
https://gitee.com/nutz/nutzboot.git
synced 2024-12-04 20:58:22 +08:00
Merge pull request #101 from swtseaman/dev
add: 新增支持读取jar包外部application.properties为绝对路径
This commit is contained in:
commit
f2503a23e9
@ -109,6 +109,7 @@ public class MainLauncher {
|
|||||||
* [haoqoo](https://github.com/haoqoo)(提交velocity)
|
* [haoqoo](https://github.com/haoqoo)(提交velocity)
|
||||||
* [鱼夫](https://gitee.com/yustory)(正在踩NB+U家三剑客的坑)
|
* [鱼夫](https://gitee.com/yustory)(正在踩NB+U家三剑客的坑)
|
||||||
* [幸福的旁边](https://github.com/happyday517)(提交caffeine方法缓存)
|
* [幸福的旁边](https://github.com/happyday517)(提交caffeine方法缓存)
|
||||||
|
* [文涛](https://github.com/swtseaman) (新增支持加载外部配置文件)
|
||||||
* 还有您的名字哦,告知我们吧
|
* 还有您的名字哦,告知我们吧
|
||||||
|
|
||||||
## 采用NutzBoot的公司
|
## 采用NutzBoot的公司
|
||||||
|
@ -3,6 +3,8 @@ package org.nutz.boot.config.impl;
|
|||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.io.UnsupportedEncodingException;
|
||||||
|
|
||||||
import org.nutz.ioc.impl.PropertiesProxy;
|
import org.nutz.ioc.impl.PropertiesProxy;
|
||||||
import org.nutz.lang.Streams;
|
import org.nutz.lang.Streams;
|
||||||
@ -16,6 +18,42 @@ import org.nutz.lang.Streams;
|
|||||||
*/
|
*/
|
||||||
public class PropertiesConfigureLoader extends AbstractConfigureLoader {
|
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<String> getFiles(String path, String extName) {
|
||||||
|
ArrayList<String> 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 {
|
public void init() throws Exception {
|
||||||
// 首先, 确定一些从什么路径加载配置文件,默认值application.properties
|
// 首先, 确定一些从什么路径加载配置文件,默认值application.properties
|
||||||
String path = envHolder.get("nutz.boot.configure.properties_path", "application.properties");
|
String path = envHolder.get("nutz.boot.configure.properties_path", "application.properties");
|
||||||
@ -29,7 +67,7 @@ public class PropertiesConfigureLoader extends AbstractConfigureLoader {
|
|||||||
}
|
}
|
||||||
// 如果当前文件夹存在application.properties,读取之
|
// 如果当前文件夹存在application.properties,读取之
|
||||||
try {
|
try {
|
||||||
File tmp = new File(path);
|
File tmp = new File(getPath(path));
|
||||||
if (tmp.exists() && tmp.canRead()) {
|
if (tmp.exists() && tmp.canRead()) {
|
||||||
try (FileInputStream ins = new FileInputStream(tmp)) {
|
try (FileInputStream ins = new FileInputStream(tmp)) {
|
||||||
conf.load(Streams.utf8r(ins), false);
|
conf.load(Streams.utf8r(ins), false);
|
||||||
@ -38,6 +76,23 @@ public class PropertiesConfigureLoader extends AbstractConfigureLoader {
|
|||||||
}
|
}
|
||||||
catch (Throwable e) {
|
catch (Throwable e) {
|
||||||
}
|
}
|
||||||
|
// 如果conf内含有configDir配置,则读取configDir下的所有配置文件
|
||||||
|
// 配置示例: configDir=config, 那么读取的就是jar包当前目录下config子目录下的所有properties文件
|
||||||
|
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));
|
||||||
|
if (tmp.exists() && tmp.canRead()) {
|
||||||
|
try (FileInputStream ins = new FileInputStream(tmp)) {
|
||||||
|
conf.load(Streams.utf8r(ins), false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Throwable e) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
// 也许命令行里面指定了profile,需要提前load进来
|
// 也许命令行里面指定了profile,需要提前load进来
|
||||||
PropertiesProxy tmp = new PropertiesProxy();
|
PropertiesProxy tmp = new PropertiesProxy();
|
||||||
if (args != null) {
|
if (args != null) {
|
||||||
|
Loading…
Reference in New Issue
Block a user