我有這個(gè)LoggerProducer類,它被注入到一個(gè)@Statelessbean 中以生成日志條目,如此處所述。問題是當(dāng)CustomerBean被調(diào)用(甚至沒有調(diào)用logger.info)時(shí),@Produces方法(檢索 bean 類名)以NullPointerException. 這段代碼有什么問題?@Named@Singletonpublic class LoggerProducer { @Produces public Logger produceLogger(InjectionPoint injectionPoint) { return LoggerFactory.getLogger( injectionPoint.getBean().getBeanClass()); // <- error here } }注入記錄器的bean:import org.slf4j.Logger;@Stateless@LocalBean@Namedpublic class CustomerBean { @Inject private Logger logger; ...... logger.info("some message");
1 回答

猛跑小豬
TA貢獻(xiàn)1858條經(jīng)驗(yàn) 獲得超8個(gè)贊
假設(shè)它injectionPoint不為空(在你的生產(chǎn)者方法中),你可以試試這個(gè):
@Produces
Logger createLogger(InjectionPoint injectionPoint) {
return LoggerFactory.getLogger( injectionPoint.getMember().getDeclaringClass().getName() );
}
添加回答
舉報(bào)
0/150
提交
取消