【发布时间】:2016-05-03 07:51:14
【问题描述】:
我有一个场景,我正在尝试在我的应用程序中实现错误代码机制。这是我目前正在遵循的方法。
Properties prop = new Properties();
InputStream input = null;
try {
String filename = "ErrorCode.properties";
input = getClass().getClassLoader().getResourceAsStream(filename);
if (input == null) {
log.debug("Sorry, unable to find " + filename);
return null;
}
prop.load(input);
Enumeration<?> e = prop.propertyNames();
while (e.hasMoreElements()) {
String key = (String) e.nextElement();
String value = prop.getProperty(key);
log.debug("Key : " + key + ", Value : " + value);
}
但是我需要很多不同类的错误代码,我不想一直在不同的类中编写上述代码。
是否有任何其他方法可以将属性文件初始化一次并在类中的任何位置使用它。
我该如何实现?
有哪些不同的方法可以实现这一目标?
我用的是spring,spring有没有办法实现这个?
我愿意接受任何其他机制,而不是属性文件。
【问题讨论】:
-
如果是错误代码,您可能需要对它们进行本地化 - 在这种情况下,请在此处查看解决方案:stackoverflow.com/questions/6246381/…
标签: java spring properties