通過.properties文件配置參數的好處最重要的就是,如果在維護時這個參數被頻繁的修改,只需要在.properties文件裏面修改一次即可。
在src文件夾下新建.properties文件,在裏面添加變量和值。
那麼如何在代碼中使用.properties文件的變量呢?
private static String wsUrl;
static{
InputStream inputStream = SpaceInfoImpl.class.getResourceAsStream("/Constant.properties");
Properties p = new Properties();
try {
p.load(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
wsUrl=p.getProperty("wsurl");
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
這樣.properties文件裏的wsurl就被取出來了。