2011-02-05 43 views
10

是否可以將屬性文件轉換爲枚舉。作爲枚舉的java屬性文件

我有一個很多設置的propoerty文件。例如

equipment.height 
equipment.widht 
equipment.depth 
and many more like this and not all are as simple as the example 

開發人員必須知道密鑰才能獲得該房產的價值。而是可以做些什麼,開發人員可以在這裏鍵入MyPropertyEnum。和密鑰列表將顯示在IDE中,就像它顯示一個枚舉

MyPropertyEnum.height 

回答

16

我經常使用屬性文件+枚舉組合。這裏有一個例子:

public enum Constants { 
    PROP1, 
    PROP2; 

    private static final String PATH   = "/constants.properties"; 

    private static final Logger logger   = LoggerFactory.getLogger(Constants.class); 

    private static Properties properties; 

    private String   value; 

    private void init() { 
     if (properties == null) { 
      properties = new Properties(); 
      try { 
       properties.load(Constants.class.getResourceAsStream(PATH)); 
      } 
      catch (Exception e) { 
       logger.error("Unable to load " + PATH + " file from classpath.", e); 
       System.exit(1); 
      } 
     } 
     value = (String) properties.get(this.toString()); 
    } 

    public String getValue() { 
     if (value == null) { 
      init(); 
     } 
     return value; 
    } 

} 

現在,你還需要一個屬性文件(我ofter放置在SRC,所以它被打包成JAR),只有當你在枚舉使用的屬性。例如:

constants.properties:

#This is property file... 
PROP1=some text 
PROP2=some other text 

現在我經常使用類的靜態進口,我想用我的常量:

import static com.some.package.Constants.*; 

和示例使用

System.out.println(PROP1); 
+1

謝謝,我會試一試並回復給您。 – user373201 2011-02-05 22:55:25

+0

非常感謝,不,我需要的是 – user373201 2011-02-06 00:13:46

2

不,我不這麼認爲。枚舉是在編譯時創建的,因此它們的元素數量不能隨屬性文件中的值而變化。

這很可能是你需要一個靈活的結構在運行時構建 - 也許是一個關聯數組。

5

Java有靜態類型。這意味着,你不能動態創建類型。所以,答案是沒有。您無法將屬性文件轉換爲枚舉。

你可以做的是從該屬性文件生成一個enum。或者,使用字典(圖)喜歡的東西來訪問屬性:

equipment.get("height"); 
+1

感謝您的回覆。我如何從該屬性文件生成一個枚舉。我並不需要動態的枚舉。我不介意在從屬性文件中添加或刪除屬性時更新枚舉常量。我會嘗試一下atlantis的建議,看看它是否可行。我需要的所有IDE才能夠顯示屬性列表 – user373201 2011-02-05 22:55:06

1

號好了,我想你可以,如果你可以編譯屬性文件成一個Java類(或枚舉)。我找不到這樣的東西(但會非常酷)

0

您可以使用生成的代碼將屬性文件轉換爲枚舉。您可以在編譯程序之前靜態執行此操作,也可以在運行時使用Compiler API。這樣做會增加很多複雜性,並且通常使用Map所建議的更簡單。

1

我可以想象,獲得IDE中所有屬性的一種方法是定義一個Enum所有這些,像這樣:

public enum Settings 
{ 
    EQUIPMENT_HEIGHT("equipment.height", "0"), 

    EQUIPMENT_WIDTH("equipment.width", "0"), 

    EQUIPMENT_DEPTH("equipment.depth", "0"); 

    private String property; 

    private String value; 

    Settings(final String aProperty, final String aValue) 
    { 
     property = aProperty; 
     value = aValue; 
    } 

    public String getProperty() 
    { 
     return property; 
    } 

    public String getValue() 
    { 
     return value; 
    } 

    private void setValue(final String aValue) 
    { 
     value = aValue; 
    } 

    public static void initialize(final Properties aPropertyTable) 
    { 
     for(final Settings setting : values()) 
     { 
     final String key = setting.getProperty(); 
     final String defaultValue = setting.getValue(); 
     setting.setValue(aPropertyTable.getProperty(key, defaultValue)); 
     } 
    } 
} 

enum的初始化是自我解釋(方法initialize())。

之後,你可以使用它像這樣:

Settings.EQUIPMENT_HEIGHT.getValue(); 

增加新的屬性只是添加新的枚舉常數。

0

有人問到關於枚舉的問題,我想創建一個基於屬性的枚舉。我放棄了這個想法,因爲這個枚舉需要是動態的,如果我們得到一個新的錯誤代碼,它應該被添加到屬性文件中。 可悲的是我看不出用enum做到這一點,所以我選擇了不同的路徑,因爲我看到每個人都建議基於地圖的解決方案。我創建了一個只讀取屬性文件的單例,並對關鍵字進行響應以返回值,而不是使用枚舉。

屬性文件:

C102 = Blablabla1 
C103 = Blablabla2 
C104 = Blablabla3 

單身代碼:

package mypackage; 

import java.io.FileInputStream; 
import java.io.IOException; 
import java.util.HashMap; 
import java.util.Map.Entry; 
import java.util.Properties; 

public class ResponseValidationTypeCodes { 

private final HashMap<String, String> codes; 

private static ResponseValidationTypeCodes instance; 

public static ResponseValidationTypeCodes getInstance() { 
    if (instance == null) { 
     instance = new ResponseValidationTypeCodes(); 
    } 
    return instance; 
} 

private ResponseValidationTypeCodes() { 
    super(); 
    codes = new HashMap<String, String>(); 
    initEntry(); 
} 

private void initEntry() { 
    Properties prop = new Properties(); 
    try { 
     prop.load(new FileInputStream(
       "src/main/resources/validationcodes.properties")); 
     for (Entry<Object, Object> element : prop.entrySet()) { 
      codes.put(element.getKey().toString(), element.getValue() 
        .toString()); 
     } 

    } catch (IOException ex) { 
     ex.printStackTrace(); 
    } 
} 

public String getValueByCode(String code) { 
    return codes.get(code); 
} 
} 

得到的值,只需撥打:

ResponseValidationTypeCodes.getInstance() 
      .getValueByCode("C102"); 

的初始屬性讀取只運行一次。所以,只需在發生某些更改時擴展該屬性,然後重新部署您的資料。我希望這對於那些願意使用一些替代枚舉的人有所幫助。

0
public enum ErrorCode{ 


    DB_ERROR(PropertiesUtil.getProperty("DB_ERRROR_CODE"), PropertiesUtil.getProperty("DB_ERROR")), 
    APP_ERROR(PropertiesUtil.getProperty("APPLICATION_ERROR_CODE"), PropertiesUtil.getProperty("APPLICATION_ERROR")), 
    ERROR_FOUND(PropertiesUtil.getProperty("ERROR_FOUND_CODE"), PropertiesUtil.getProperty("ERROR_FOUND")); 


    private final String errorCode; 
    private final String errorDesc; 



    private ErrorCode(String errorCode, String errorDesc) { 
     this.errorCode = errorCode; 
     this.errorDesc = errorDesc; 
    } 

    public String getErrorDesc() { 
     return errorDesc; 
    } 

    public String getErrorCode() { 
     return errorCode; 
    } 

    public static String getError(String errorCode) 
    { 
     System.out.println("errorCode in Enum"+errorCode); 
     System.out.println(java.util.Arrays.asList(ErrorCode.values())); 
     for (ErrorCode errorEnum : ErrorCode.values()) { 
      System.out.println(errorEnum.errorCode); 
      System.out.println(errorEnum.errorDesc); 
     if ((errorEnum.errorCode).equals(errorCode)) { 
      return errorEnum.getErrorDesc(); 
     } 
     } 
     return ERROR_FOUND.getErrorDesc(); 

    } 


public class PropertiesUtil { 
static Properties prop = new Properties(); 

    static{ 

     try { 

       InputStream inputStream = 
         PropertiesUtil.class.getClassLoader().getResourceAsStream("db.properties"); 

      prop.load(inputStream); 
    }catch(Exception e) 
     { 
     e.printStackTrace(); 
     } 
    } 



     public static PropertiesUtil getInstance() 
     { 

      return new PropertiesUtil(); 
     } 

     public Properties getProperties() 
     { 
      return prop; 
     } 

     public static String getProperty(String key) 
     { 
      return prop.getProperty(key); 
     } 


}