是否可以将属性文件转换为枚举。
我有一个包含很多设置的属性文件。例如
equipment.height
equipment.widht
equipment.depth
and many more like this and not all are as simple as the example
开发商必须知道钥匙才能获得房产的价值。相反,开发人员可以键入 MyPropertyEnum 来执行某些操作。键列表将显示在 IDE 中,就像 Enum 中显示的那样
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;
}
}
现在您还需要一个属性文件(我经常将其放在 src 中,因此将其打包到 JAR 中),其属性与您在 enum 中使用的属性相同。例如:
常量.属性:
#This is property file...
PROP1=some text
PROP2=some other text
现在,我经常在想要使用常量的类中使用静态导入:
import static com.some.package.Constants.*;
和一个示例用法
System.out.println(PROP1);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)