I have this LoggerProducer
class that's injected in a @Stateless
bean to generate log entries as explained here.
Problem is that when CustomerBean
is invoked (without even calling logger.info
), the @Produces
method (that retrieves the bean class name) fails with NullPointerException
. What is wrong with this code?
@Named
@Singleton
public class LoggerProducer {
@Produces
public Logger produceLogger(InjectionPoint injectionPoint) {
return LoggerFactory.getLogger(
injectionPoint.getBean().getBeanClass()); // <- error here
}
}
The bean that injects the logger:
import org.slf4j.Logger;
@Stateless
@LocalBean
@Named
public class CustomerBean {
@Inject
private Logger logger;
......
logger.info("some message");
Assuming that
injectionPoint
is not null (in your producer method), you can try this: