7
votes

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

1

1 Answers

1
votes

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.