Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Injecting a Logger into an Ejb using CDI

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

like image 614
fabpicca Avatar asked Oct 31 '12 17:10

fabpicca


People also ask

What does the @inject annotation do?

The annotation @Inject allows Dagger to call the constructor of the injected class to create an instance of a class, twitterApi . However, there are some cases that we may not call a constructor directly. For example: Interfaces.

Why use CDI Java?

CDI is preferred because it allows apps of large (arbitrary?) horizontal and vertical scales to share contexts, dependencies, and therefore data.

What is a CDI bean?

But what is a CDI bean? A CDI bean is a POJO, plain old java object, that has been automatically instantiated by the CDI container, and is injected into all, and any qualifying injection points in the application. The CDI container initiates the bean discovery process during deployment.


1 Answers

There are some inconsistencies between your text and the structure of your EAR, i.e. you talk about a utils.jar but there is no such .jar in your EAR structure diagram.

Nonetheless, you mention that your @EJB is packaged inside loader.jar which in turn is in the /lib folder of your EAR. Since loader.jar is your ejb-jar it shouldn't be under /lib, but should be at the top level of your EAR.

In the application.xml:

<?xml version="1.0" encoding="UTF-8"?>
<application xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/application_6.xsd" version="6">
    <module>
        <ejb>loader.jar</ejb>
    </module>
    ...
    <library-directory>lib</library-directory>
</application>

Hopefully this resolves your issue.

like image 199
rdcrng Avatar answered Sep 27 '22 18:09

rdcrng