parent
bdd9b218a1
commit
074ba3c217
@ -1,37 +0,0 @@
|
||||
package com.dsideal.Config;
|
||||
|
||||
import com.jfinal.kit.Prop;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class PropKit {
|
||||
private static final Logger logger = LoggerFactory.getLogger(PropKit.class);
|
||||
|
||||
public static String getEnvPrefix() {
|
||||
String myEnvVar = System.getenv("WORKING_ENV");
|
||||
if (myEnvVar == null) {
|
||||
myEnvVar = "dev";
|
||||
}
|
||||
return myEnvVar;
|
||||
}
|
||||
|
||||
public static Prop prop;
|
||||
public static String configFile;
|
||||
|
||||
static {
|
||||
//加载配置文件
|
||||
configFile = "application_{?}.yaml".replace("{?}", getEnvPrefix());
|
||||
prop = new YamlProp(configFile);
|
||||
}
|
||||
|
||||
public static String get(String key) {
|
||||
if (!prop.containsKey(key)) {
|
||||
throw new RuntimeException("没有找到配置文件" + configFile + "中的键值:" + key);
|
||||
}
|
||||
return prop.get(key);
|
||||
}
|
||||
|
||||
public static int getInt(String key) {
|
||||
return prop.getInt(key);
|
||||
}
|
||||
}
|
@ -1,47 +0,0 @@
|
||||
package com.dsideal.Config;
|
||||
|
||||
import com.jfinal.kit.Prop;
|
||||
import org.yaml.snakeyaml.Yaml;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
|
||||
public class YamlProp extends Prop {
|
||||
public YamlProp(String yamlFile) {
|
||||
setYaml(yamlFile);
|
||||
}
|
||||
|
||||
private void setYaml(String yamlFile) {
|
||||
LinkedHashMap map = null;
|
||||
Properties properties = new Properties();
|
||||
InputStream in;
|
||||
try {
|
||||
Yaml yaml = new Yaml();
|
||||
in = YamlProp.class.getClassLoader().getResourceAsStream(yamlFile);
|
||||
map = yaml.loadAs(in, LinkedHashMap.class);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
setProperties(properties, map, "");
|
||||
this.properties = properties;
|
||||
}
|
||||
|
||||
private static void setProperties(Properties properties, Map map, String prefix) {
|
||||
for (Object key : map.keySet()) {
|
||||
Object value = map.get(key);
|
||||
if (key == null) {
|
||||
continue;
|
||||
}
|
||||
if (value instanceof Map) {
|
||||
setProperties(properties, (Map) value, prefix + key + ".");
|
||||
} else {
|
||||
if (value == null) {
|
||||
continue;
|
||||
}
|
||||
properties.setProperty(prefix + key, value.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue