背景:
能不用spring就不用侨歉。。。兼顧了環(huán)境變量和文件配置。
import java.io.InputStream;
import java.util.Map;
import java.util.Properties;
/**
* 兩種模式思恐,一種讀取包內配置文件,二種讀取環(huán)境變量膊毁,通過命令行指定胀莹,默認env.property
*/
public class Configuration {
public static Properties properties = new Properties();
public static void init (String[] args){
String cmd = args.length > 0 ? args[0] : "env.property";
//使用環(huán)境變量
if (cmd.equals("env")){
for (Map.Entry<String, String> entry : System.getenv().entrySet()) {
properties.setProperty(entry.getKey(), entry.getValue());
}
}else{
try (InputStream is = Configuration.class.getClassLoader().getResourceAsStream(cmd)) {
properties.load(is);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("Configuration init fail!");
}
}
}
}