Note that there are some explanatory texts on larger screens.

plurals
  1. POInjecting a Logger into an Ejb using CDI
    primarykey
    data
    text
    <p>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.</p> <p>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.</p> <p>Then I've created a small logger class, which actually wraps log4j:</p> <pre><code> 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()); } } </code></pre> <p>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.</p> <p>Now, in the stateless Singleton Bean I've mentioned before (the loader)</p> <p>I've wrote just this</p> <pre><code> 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"); } } </code></pre> <p>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.</p> <p>So far my EAR structure is as follows:</p> <pre><code> EAR | +---lib | +--loader.jar | +--utils.jar | +--log4j.xx.jar | +--MyStatelessSessionBean.jar </code></pre> <p>Thanks a lot for your help </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload