1 Reply Latest reply on Feb 4, 2009 10:37 AM by jaikiran

    @EJB does not work in JSF backing bean constructor

      I have a situation where I use @EJB annotation in a JSF backing bean and sometimes it is null and other time it is not.

      I am converting an application from jboss-eap-4.3 to jboss-5.0.0.GA. In 4.3 I used JNDI lookup.

      The problem was the injected reference to the stateless session bean was being called one time from inside the constructor for the backing bean. That is when it would be NULL. All other references worked fine.

      Is that correct behavior?

        • 1. Re: @EJB does not work in JSF backing bean constructor
          jaikiran

           

          "perfectionist" wrote:


          The problem was the injected reference to the stateless session bean was being called one time from inside the constructor for the backing bean.



          I haven't used JSF, but i think you should be using the injected reference after @PostConstruct and not in the constructor of the managed bean.

          public class MyBackingBean ...
          {
          
           @EJB
           private MyEJB ejb;
          
           public MyBackingBean()
           {
           ... // don't use the bean here, its not yet injected
           }
          
           @PostConstruct
           public void myPostConstruct()
           {
           // use the bean here, since this is called after
           // resources have been injected
           ejb.doSomething();
           }
          ...