我有一個場景,我試圖在我的應用程序中實現錯誤代碼機制。這是我目前遵循的方法。如何加載屬性文件一次並在整個應用程序中使用它在java中
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);
}
但我需要很多不同的類中的錯誤代碼,我不想一直在不同的類中編寫上面的代碼。
是否有任何其他方式初始化屬性文件一次,並在類中的任何地方使用它。
我該如何做到這一點?
實現此目的有哪些不同的可能方法?
我使用的是春天,有什麼辦法可以在春天實現這個嗎?
我打開任何其他機制,而不是屬性文件。
如果是錯誤代碼,您可能需要進行本地化 - 在這種情況下,請查看以下解決方案:http://stackoverflow.com/questions/6246381/getting-localized-message-from-resourcebundle-via-annotations-在 - 彈簧 - framewor – Jan