Glassfish Bean Reference Lookup Fails
===

Wierd problem here. I have a bean with a remote interface declared and an implementation defined as a Stateless bean. Since I want to be able to replace the bean with a different bean depending on the implementation requirements, I have an ejb-reference declared in glassfish-web.xml.

I can successfully inject the bean if I use the name syntax to refer to the reference name like    @EJB(name = "BeanReference"). I can also do a lookup: new InitialContext().lookup("java:comp/env/BeanReference").

The wierd thing happens when I don't have any injections at all (no @EJB). Then the lookup fails with "javax.naming.NameNotFoundException: No object bound to name java:comp/env/BeanReference". If I list the pairs in "java:comp/env" it confirms that the reference doesn't exist unless an @EJB injection occurs somewhere in the application. I have checked the jndi-name entry and confirmed it matches the output from Glassfish during initialization.

I also tried using @Local instead of @Remote and get the same results. I prefer the @Remote since in an actual deployment the MyBean implementation will likely reside on a remote glassfish instance. It is local only for development and testing.

Using Glassfish 3.1.1 and NetBeans 7.1.2. Code snippets below...exception handling omitted for clarity.

    @Remote
    public interface MyBean {
        public String doSomething();
    }

    @Stateless
    public class MyBeanImpl implements MyBean {
        @Override
        public String doSomething() {
            return "something";
        }
    }

    @Stateless
    @LocalBean
    public class MyOtherBean {
        public MyOtherBean() {
            Context ctx = new InitialContext();
            MyBean myBean = (MyBean)ctx.lookup("java:comp/env/BeanReference");
            String rc = myBean.doSomething();
            System.out.println("rc = " + rc);
        }
    }

    <ejb-ref>
        <ejb-ref-name>BeanReference</ejb-ref-name>
        <jndi-name>java:global/appName/MyBeanImpl!com.test.bean.MyBean</jndi-name>
    </ejb-ref>
