6 Replies Latest reply on Nov 12, 2012 7:46 AM by Mylos Kathos

    Resource Injection in plain Java class fails

    Mylos Kathos Master

      Hi all !

      I'm having some issues when injecting JMS resources in my application. I have a JMS client Servlet where resource injection works like a charm:

       

      public class JMSServlet extends HttpServlet {

       

          @Resource(mappedName = "java:jboss/exported/jms/queue/mainQueue")

          private Queue queue;

       

          @Resource(mappedName = "java:/ConnectionFactory")

          private ConnectionFactory cf;

          . . . . .

      }

       

      Now I'd like to move all the JMS stuff in an Utility class, which just contains the same code as the Servlet:

       

      public class JMSUtility  {

       

          @Resource(mappedName = "java:jboss/exported/jms/queue/mainQueue")

          private Queue queue;

       

          @Resource(mappedName = "java:/ConnectionFactory")

          private ConnectionFactory cf;

          . . . . .

          public void sendMessage() {

               // NPE! Connection factory is null !

          }

      }

       

      Now if I try to invoke the JMSUtility from the Servlet with a simple

      new JMSUtility().sendMessage();

      I get a NPE because injections are null in the method sendMessage().

      Forgive my ignorance, but shouldn't that work the same either in a Servlet or in a Java class -provided that the Java class is running on the server ?

      Am I missing something, apparentely silly that I cannot see ? for completeness, I'm running this test against JBoss AS 7.1.1

      Thanks

      Mylos