[英]How to load properties from config.properties in multithreaded environment in JAVA
我有程序從文件config.properties加載主類屬性,如下所示:
public class DirectoryWatcher {
public static String FOLDER = null;
Properties prop = new Properties();
prop.load(new FileInputStream(new File(configPath)));
FOLDER = prop.getProperty("FOLDER");
}
許多線程需要FOLDER,因為我將它設置為公共靜態 ,因此線程可以使用它。
我不喜歡這種編程,我正在尋找一些最佳實踐實現。
你能給我一些更好的建議嗎? 謝謝。
對我來說,這就足夠了
public class DirecoryWatcher{
private static String FOLDER;
public static synchronized getFolder(){
if(FOLDER == null){
// FOLDER = your loading code
}
return FOLDER;
}
}
確保將從文件中讀取的值分配給靜態字段,因此請確保只讀取一次。
此外,同步方法是訪問資源的一種很好的做法,對於這種情況,它並不是完全必需的,因為您只是在閱讀文件。
您還可以使此方法可擴展,以讀取作為參數給出的任何屬性。 為了清晰起見,我硬編碼了FOLDER。
public class DirectoryWatcher{
private static Map<String,String> properties = new HashMap<String,String>();
public static synchronized getValueFor(String prop){
String result = null;
if( !properties.keySet().contains(prop)){
result = // your loading code
properties.put(prop, result);
}else{
result = properties.get(prop);
}
return result;
}
}
此代碼將為您提供線程安全性並支持任何給定數量的屬性。 它還增強了代碼的封裝,你可以為它添加一些邏輯(你不只是暴露文件的內容)。
此外,在這種情況下,在首次需要屬性之前不會加載屬性。 如果從未使用過屬性,則不會讀取它。 這可以提高內存使用率,不會浪費內存在您不需要的值上。
另一個需要考慮的重要事項是,通過此實現,您的屬性加載器類可以非常輕松地處理錯誤和異常。 使用另一種方法,您將處理問題的責任委托給請求該屬性的對象。
你可以做到最終:
private static final Properties prop = new Properties();
public static final String FOLDER;
static {
try {
prop.load(new FileInputStream(new File(configPath)));
} catch (IOException ex) {
//outch => log and exit?
}
FOLDER = prop.getProperty("FOLDER");
}
這將確保從任何線程都可以看到它。 如果您擁有多個屬性,則還可以使用此示例使用枚舉並且是線程安全的。
也許你可以使用Singleton模式來解決這個問題?
如果您有大量要加載的信息,也可以對文件夾使用延遲加載。
你可以像這樣定義一個簡單的屬性文件閱讀器
public class LoadDataFromPropertiesFile {
public final static Properties loadPropertiesFile(String fileName) {
ClassLoader loader = Thread.currentThread().getContextClassLoader();
InputStream inStream=null;
try {
inStream = loader.getResourceAsStream(fileName);
if (inStream == null) {
throw new RuntimeException("Couldn't find " + fileName + "in class path");
}
Properties prop = new Properties();
prop.load(inStream);
return prop;
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
}finally {
if (inStream != null) {
try {
inStream.close();
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
}
}
特定屬性文件可以如下所示。
public class DirectoryWatcher {
public static final Properties directoryWatcher;
static {
directoryWatcher = LoadDataFromPropertiesFile.loadPropertiesFile("config.properties");
}
}
您可以定義為ENUM類型的屬性...您可以在此處列出您的屬性
public enum DirectoryProperties {
FOLDER("FOLDER","Directory Type Folder"),
IMAGE("IMG","Image File");
;
DirectoryProperties(String code, String description) {
this.code = code;
this.description = description;
}
public String getCode() {
return code;
}
public String getDescription() {
return description;
}
private String code;
private String description;
}
您可以在任何線程中使用您的屬性。 像這樣
DirectoryWatcher.directoryWatcher.getProperty(DirectoryProperties.FOLDER.getCode());
您可以根據需要使用說明。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.