私の現在のビルド リーダーは、理論的には素晴らしいアイデアを持っています。つまり、Spring が管理する Bean を取り込んで、それらを使用して標準のログ ファイル以外のさまざまなソースにエラーを記録するカスタム Log4J アペンダーを構築します。ただし、アプリケーション コンテキストを使用して起動時に初期化されるシングルトンを作成する (コードはすぐに説明します) 以外に、Log4J アペンダーで Spring マネージド Bean を取得する他のオプションは考えられないようです。
public class SpringSingleton implements ApplicationContextAware {
private static ApplicationContext context;
public SpringSingleton() {
super();
}
public static ApplicationContext getContext() {
return SpringSingleton.context;
}
public void setApplicationContext(ApplicationContext context) {
if(SpringSingleton.context != null) {
throw new IllegalStateException("Context is already set!");
}
SpringSingleton.context = context;
}
}
理想的には、これらのプロパティは依存性注入を介して Spring の Bean のように設定できます。初期化されたアペンダーの数に関係なく、Bean 参照は決して変更されません。何か案は?