私はシナリオを持っています。アプリケーションにエラーコードのメカニズムを実装しようとしています。プロパティファイルを一度ロードして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-春のフレームワーク – Jan