シングルトンアプローチに従って実装されたクラスにロガーオブジェクトを注入しようとしています。
コードはほぼ次のようになります。
Logger
クラス:
public class LoggerFactory {
@Produces
public Logger getLogger(InjectionPoint caller){
return Logger.getLogger(caller.getMember().getDeclaringClass().getName());
}
}
次に、ロガーを必要とし、シングルトンパターンを実装するクラスを作成します。
public class MySingleton{
@Inject
private Logger logger;
private MySingleton instance;
/*
* Private constructor for singleton implementation
*/
private MySingleton(){
logger.info("Creating one and only one instance here!");
}
public MySingleton getInstance(){
if(instance == null) {
instance = new MySingleton();
}
return instance;
}
}
(Glassfish 3.1.2.2で)コードを実行すると、ロガーを使用しようとするとすぐにNPEが取得されます。私が間違っていること(beans.xml
ファイルが配置されている)?また、オブジェクト@Inject
のセッターメソッドを使用してみましたが、うまくいきませんでした。Logger