I'm struggling a bit to put together what I thought was a simple task. I have a Stateless Singleton Bean, that I'm going to use as a "loader" for my app. The bean is included in a Jar (loader.jar) file and resides in the lib folder of my EAR.
In the root of the EAR resides another jar, containing the implementation of a Stateless Session Bean that is going to be used by my application.
Then I've created a small logger class, which actually wraps log4j:
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.enterprise.context.RequestScoped;
import javax.enterprise.inject.Produces;
import javax.enterprise.inject.spi.InjectionPoint;
import javax.inject.Named;
import org.apache.log4j.Logger;
public class LoggerFactory {
private Logger logger;
public LoggerFactory(){
logger = Logger.getLogger(this.getClass().getName());
}
@Produces Logger getLogger(InjectionPoint caller){
return Logger.getLogger(caller.getMember().getDeclaringClass().getName());
}
}
Then I've placed this inside a jar, that I've named as utils.jar. In this jar, following CDI specifications, I've created inside the META-INF folder an empty beans.xml file. The utils.jar resides in the lib folder of my EAR.
Now, in the stateless Singleton Bean I've mentioned before (the loader)
I've wrote just this
import javax.annotation.PostConstruct;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import javax.inject.Inject;
import org.apache.log4j.Logger;
@Startup
@Singleton
public class Core {
@Inject
private Logger logger;
@PostConstruct
void init(){
logger.info("Core started");
}
}
when I deploy my app on glassfish 3.1.2 I get a plain NPE at the first invocation of logger in my Loader bean. This makes me think that CDI invocation is not happening at all, but I sincerely cannon understand what I'm doing wrong.
So far my EAR structure is as follows:
EAR
|
+---lib
| +--loader.jar
| +--utils.jar
| +--log4j.xx.jar
|
+--MyStatelessSessionBean.jar
Thanks a lot for your help