0

I am using JBoss As 7 and Java EE 6. I have an empty beans.xml

I have a logger Producer like this..

@Singleton
@Startup
public class LoggerProducer {

     private static Logger logger = Logger.getLogger(LoggerProducer.class.getName());

    @Produces
    public Logger produceLogger(final InjectionPoint injectionPoint) {  
        final String injectingClass = injectionPoint.getMember().getDeclaringClass().getName();
        return Logger.getLogger(injectingClass);  
    }  

}

In my class i Inject as follows...

@Inject
Logger logger;

I import java util logger in each case

import java.util.logging.Logger;

Everything deploys correctly however the Injection of the logger is failing and I get a runtime NullPointer if i try to use the injected logger

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • ..you have to modify the log4j.xml file.. as per your requirements – idiotduffer Sep 19 '17 at 10:57
  • hi @idiotduffer, thank you for your reply. I am not using log4j but I am using java util logging. This uses the auto generated logging.properties file in /standalone/configuration/logging.properties – StackOverflow Account Sep 19 '17 at 11:36
  • Do you have your beans.xml in the right place? Do you have a WAR or an EAR file? Do you also inject other objects that work? – awagenhoffer Sep 19 '17 at 13:24

1 Answers1

1

Thanks to all for replies.

I have beans.xml in the right place (WEB-INF). It is packaged as a .war.

The issue was my own coding fault in the class(bean) i was injecting the logger into. I had mistakenly used a constructor to initialise the class which thus tried to use the logger before CDI had a chance to inject it

The fix was to replace the constructor with a @PostConstruct method and all worked well.

@MyAnnotation
@Singleton
public class MtHammer implements Hammer {

    .....

    @Inject
    Logger logger2;


    @PostConstruct
    private void startup() {

        initialise();

    }

     private void initialise() {
            logger.info("logger...Initialising ...");
      ....
        }
  • ..place your bean.xml in META-INF folder, for war file it should be in META-INF and for EAR file it should be in WEB-INF. – idiotduffer Oct 09 '17 at 04:56