From 664ad9e4aa69bdde239a83e956f87772c99b0e62 Mon Sep 17 00:00:00 2001 From: Redkale Date: Sun, 18 Dec 2022 17:35:30 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8DApplication.replaceValue?= =?UTF-8?q?=E7=A9=BA=E6=8C=87=E9=92=88=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/org/redkale/boot/Application.java | 62 ++++++++++--------- 1 file changed, 32 insertions(+), 30 deletions(-) diff --git a/src/main/java/org/redkale/boot/Application.java b/src/main/java/org/redkale/boot/Application.java index 64942dfa3..fb2de1129 100644 --- a/src/main/java/org/redkale/boot/Application.java +++ b/src/main/java/org/redkale/boot/Application.java @@ -254,6 +254,36 @@ public final class Application { this.config = config; this.configFromCache = "true".equals(config.getValue("[config-from-cache]")); this.environment = new Environment(this.envProperties); + //设置APP_HOME、APP_NAME + final File root = new File(System.getProperty(RESNAME_APP_HOME)); + this.resourceFactory.register(RESNAME_APP_TIME, long.class, this.startTime); + this.resourceFactory.register(RESNAME_APP_HOME, Path.class, root.toPath()); + this.resourceFactory.register(RESNAME_APP_HOME, File.class, root); + this.resourceFactory.register(RESNAME_APP_HOME, URI.class, root.toURI()); + File confFile = null; + try { //设置APP_HOME + this.resourceFactory.register(RESNAME_APP_HOME, root.getCanonicalPath()); + if (System.getProperty(RESNAME_APP_HOME) == null) { + System.setProperty(RESNAME_APP_HOME, root.getCanonicalPath()); + } + this.home = root.getCanonicalFile(); + this.homePath = this.home.getPath(); + String confDir = System.getProperty(RESNAME_APP_CONF_DIR, "conf"); + if (confDir.contains("://") || confDir.startsWith("file:") || confDir.startsWith("resource:") || confDir.contains("!")) { //graalvm native-image startwith resource:META-INF + this.confPath = URI.create(confDir); + if (confDir.startsWith("file:")) { + confFile = new File(this.confPath.getPath()).getCanonicalFile(); + } + } else if (confDir.charAt(0) == '/' || confDir.indexOf(':') > 0) { + confFile = new File(confDir).getCanonicalFile(); + this.confPath = confFile.toURI(); + } else { + confFile = new File(this.home, confDir).getCanonicalFile(); + this.confPath = confFile.toURI(); + } + } catch (IOException e) { + throw new RuntimeException(e); + } { //设置系统变量 AnyValue propertiesConf = this.config.getAnyValue("properties"); if (propertiesConf != null) { //设置配置文件中的系统变量 @@ -263,6 +293,7 @@ public final class Application { String propName = key.substring("system.property.".length()); if (System.getProperty(propName) == null) { //命令行传参数优先级高 String value = prop.getValue("value"); + //replaceValue调用前必须给home、name赋值 System.setProperty(propName, value == null ? value : replaceValue(value)); } } @@ -300,37 +331,8 @@ public final class Application { this.name = checkName(config.getValue("name", "")); this.resourceFactory.register(RESNAME_APP_NAME, name); System.setProperty(RESNAME_APP_NAME, name); + } - } - final File root = new File(System.getProperty(RESNAME_APP_HOME)); - this.resourceFactory.register(RESNAME_APP_TIME, long.class, this.startTime); - this.resourceFactory.register(RESNAME_APP_HOME, Path.class, root.toPath()); - this.resourceFactory.register(RESNAME_APP_HOME, File.class, root); - this.resourceFactory.register(RESNAME_APP_HOME, URI.class, root.toURI()); - File confFile = null; - try { //设置APP_HOME - this.resourceFactory.register(RESNAME_APP_HOME, root.getCanonicalPath()); - if (System.getProperty(RESNAME_APP_HOME) == null) { - System.setProperty(RESNAME_APP_HOME, root.getCanonicalPath()); - } - this.home = root.getCanonicalFile(); - this.homePath = this.home.getPath(); - String confDir = System.getProperty(RESNAME_APP_CONF_DIR, "conf"); - if (confDir.contains("://") || confDir.startsWith("file:") || confDir.startsWith("resource:") || confDir.contains("!")) { //graalvm native-image startwith resource:META-INF - this.confPath = URI.create(confDir); - if (confDir.startsWith("file:")) { - confFile = new File(this.confPath.getPath()).getCanonicalFile(); - } - } else if (confDir.charAt(0) == '/' || confDir.indexOf(':') > 0) { - confFile = new File(confDir).getCanonicalFile(); - this.confPath = confFile.toURI(); - } else { - confFile = new File(this.home, confDir).getCanonicalFile(); - this.confPath = confFile.toURI(); - } - } catch (IOException e) { - throw new RuntimeException(e); - } String localaddr = config.getValue("address", "").trim(); InetAddress addr = localaddr.isEmpty() ? Utility.localInetAddress() : new InetSocketAddress(localaddr, config.getIntValue("port")).getAddress(); this.localAddress = new InetSocketAddress(addr, config.getIntValue("port"));