java 讀取配置檔案

2021-07-14 04:15:32 字數 2430 閱讀 8238

前幾天因為配置檔案的事,線上的job掛掉了,使用的是spring載入配置檔案方式。

spring配置如下

test.properties

api-test.properties

decryptpropertyplaceholderconfigurer類如下所示。

public class decryptpropertyplaceholderconfigurer extends

讀取配置檔案類configreadertool如下所示。

public abstract class configreadertool ",propertyfile.getabsolutepath());

load(propertyfile);

} catch (final exception e)

} };

private configreadertool()

public static propertiesconfiguration reloadconfiguration(final string systembasepath,final string path) else

logutil.debug(logger,"載入配置檔案:",propertyfile.getabsolutepath());

load(propertyfile);

} catch (final exception e)

} };

return configuration; }

/*** 從配置檔案中讀取、新增配置資訊.

* * @param file 配置檔案

*/public static boolean addconfiguration(final file file) catch (final configurationexception e)

return true;

} public static string getarraytostring(final string key)

return sb.tostring().substring(0,sb.length()-1);

}return "";

}public static string get(final string key)

public static string get(final string key, final string defaultvalue)

public static boolean getboolean(final string key)

public static boolean getboolean(final string key, final boolean defaultvalue)

public static int getint(final string key)

public static int getint(final string key, final int defaultvalue)

public static double getdouble(final string key)

public static double getdouble(final string key, final double defaultvalue)

public static long getlong(final string key)

public static long getlong(final string key, final long defaultvalue)

public static listgetlist(final string key)

public static listgetlist(final string key, final listdefaultvalue)

public static string getstringarray(final string key)

}

test.properties 檔案在resources資料夾下,新增attach.account.push.day.num=8欄位。

讀取配置檔案方式如下。

configreadertool.getint("attach.account.push.day.num", 5)

配置檔案錯誤的發生是,在部署的時候,應該用的是linux伺服器上/etc/test/config 固定位置的配置檔案,但是當刪除掉專案中的配置檔案後,啟動就或有問題,因為讀取配置檔案的類,預設載入專案中的配置檔案,伺服器固定位置的配置檔案是在載入該類(configreadertool)之後,才進行載入。

總結:jvm類載入順序,以及jvm記憶體空間分配如何?下篇文章中探索了。

java讀取配置檔案

現今的程式都要求能夠讀寫配置檔案,使得程式的配置資訊能夠很方便地修改。讀取配置檔案通常使用property檔案,下面列舉了從hadoop的hdfs和本地的檔案系統讀取配置的例子。話不多說,上 讀取hdfs配置檔案 public static properties getpropertiesfromh...

java讀取配置檔案property

properties pro new properties try catch filenotfoundexception e catch ioexception e propertynames 返回屬性列表中所有鍵的列舉 enumeration enu2 pro.propertynames whi...

Java讀取Properties配置檔案

properties類繼承自hashtable類並且實現了map介面,使用鍵值對的形式來儲存屬性集。不過properties的鍵和值都是字串型別。1 load inputstream instream 此方法可以從.properties屬性檔案對應的檔案輸入流中,載入屬性列表到properties類...