作为枚举的java属性文件

问题描述:

是否可以将属性文件转换为枚举。作为枚举的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 

我经常使用属性文件+枚举组合。这里有一个例子:

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

不,我不这么认为。枚举是在编译时创建的,因此它们的元素数量不能随属性文件中的值而变化。

这很可能是你需要一个灵活的结构在运行时构建 - 也许是一个关联数组。

Java有静态类型。这意味着,你不能动态创建类型。所以,答案是没有。您无法将属性文件转换为枚举。

你可以做的是从该属性文件生成一个enum。或者,使用字典(图)喜欢的东西来访问属性:

equipment.get("height"); 
+1

感谢您的回复。我如何从该属性文件生成一个枚举。我并不需要动态的枚举。我不介意在从属性文件中添加或删除属性时更新枚举常量。我会尝试一下atlantis的建议,看看它是否可行。我需要的所有IDE才能够显示属性列表 – user373201 2011-02-05 22:55:06

号好了,我想你可以,如果你可以编译属性文件成一个Java类(或枚举)。我找不到这样的东西(但会非常酷)

您可以使用生成的代码将属性文件转换为枚举。您可以在编译程序之前静态执行此操作,也可以在运行时使用Compiler API。这样做会增加很多复杂性,并且通常使用Map所建议的更简单。

我可以想象,获得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(); 

增加新的属性只是添加新的枚举常数。

有人问到关于枚举的问题,我想创建一个基于属性的枚举。我放弃了这个想法,因为这个枚举需要是动态的,如果我们得到一个新的错误代码,它应该被添加到属性文件中。 可悲的是我看不出用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"); 

的初始属性读取只运行一次。所以,只需在发生某些更改时扩展该属性,然后重新部署您的资料。我希望这对于那些愿意使用一些替代枚举的人有所帮助。

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); 
     } 


}