我有这个LoggerProducer
类,它被注入@Stateless
bean中以生成日志条目,如here所述。
问题在于,当调用CustomerBean
(甚至不调用logger.info
)时,@Produces
方法(检索Bean类名)将失败,并返回NullPointerException
。此代码有什么问题?
@Named
@Singleton
public class LoggerProducer {
@Produces
public Logger produceLogger(InjectionPoint injectionPoint) {
return LoggerFactory.getLogger(
injectionPoint.getBean().getBeanClass()); // <- error here
}
}
注入记录器的bean:
import org.slf4j.Logger;
@Stateless
@LocalBean
@Named
public class CustomerBean {
@Inject
private Logger logger;
......
logger.info("some message");
答案 0 :(得分:4)
假设injectionPoint
不为空(在生产者方法中),则可以尝试以下操作:
@Produces
Logger createLogger(InjectionPoint injectionPoint) {
return LoggerFactory.getLogger( injectionPoint.getMember().getDeclaringClass().getName() );
}