12 Replies Latest reply on Oct 24, 2013 5:02 AM by bafco

    Injection of Simple Environment Entry into Session Bean Using @Resource Annotation

    bafco Newbie



      I'm having a rough time using @Resource annotation to inject a String into a session bean.
      Deployment archive structure:
      - web.xml
      - beans.xml
      - lib/...
      - classes/...
      I have the following class (in 'classes' folder):

      import javax.annotation.Resource;

      import javax.ejb.Stateless;

      import javax.naming.Context;

      import javax.naming.InitialContext;

      import javax.naming.NamingException;



      public class Bar {

          @Resource(lookup = "java:app/env/greeting")

          public String greeting;


          public String lookup() throws NamingException {

              Context initCtx = new InitialContext();

              return (String) initCtx.lookup("java:app/env/greeting");



      The web.xml looks like this:

      <?xml version="1.0" encoding="UTF-8" standalone="yes"?>

      <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="3.0" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">








      The problem is, for 'bar' - created instance of Bar - it holds that bar.greeting == null. However, bar.lookup().equals("Hello");

      I tried to instantiate the Bar instance using @EJB injection, using javax.inject.Provider.get() method of injected javax.enterprise.inject.Instance<Bar> instance, using BeanManager and CreationalContext, but the result is still the same.

      Making the Bar class Statefull, using the "java:comp/env" context or declaring the env-entry in ejb.jar.xml doesn't help to solve the problem.

      What am I doing wrong?


      I am using: Wildfly version: 8.0.0.Beta1, Weld version: 2.1.0.Final.