2 Replies Latest reply: Jun 5, 2013 6:17 PM by Sirish Vadala RSS

    EJB initialize issue

    Sirish Vadala
      Hello All,

      I have migrated from EJB 2.1 to EJB 3 in my JSF project and facing few issues while initializing through annotations.


      My JSF Managed Bean 1
      @ManagedBean(name = "mybean1")
      @RequestScoped
      public class MyBean1 { 
           public MyBean1 () { 
                super(); 
           }
      
           private String defaultTab = "";
           
              @EJB(name = "MyEJB") 
              private MyEJB myEJB;
      
           @javax.annotation.PostConstruct 
           public void init() {
                defaultTab = myEJB.retrieveDefaultOption(); 
           }
           
           public String loadDetail() {
                System.out.println("defaultTab::"+defaultTab);
                return "detail";
           }
      }
      My JSF Managed Bean 2
      @ManagedBean(name = "mybean2")
      @SessionScoped
      public class MyBean2 { 
           public MyBean2 () { 
                super(); 
           }
      
           public String showDetailForTracking() {
                MyBean1 myBean1 = new MyBean1();
                myBean1.loadDetail(); // Always prints defaultTab::     
                return null;
           }
      }
      When loadDetail method in bean 1 is called from bean 2 as shown in the above snippet, always the value of default tab is "" as the EJB is not getting constructed.

      Is there a way to fix this issue. Any hint on this would be highly appreciated.

      Thanks.
        • 1. Re: EJB initialize issue
          gimbal2
          Think about it: you create the MyBean instance yourself. Why do you expect the container to magically inject the EJB reference into it then?

          Use proper JSF mechanics to inject the MyBean1 instance into MyBean2! Any JSF 2 book or article would have told you how. You can use the JSF ManagedProperty annotation:
            @ManagedProperty(value="#{mybean1}")
            private MyBean1 myBean1;
          or if you're on a JEE 6 container you can invest good time into researching how to use CDI to have only one standardized injection model.

          And a final tip: you're making your life unnecessarily difficult by overriding the default bean names.
          • 2. Re: EJB initialize issue
            Sirish Vadala
            Thanks for the response!

            We are using JEE 6 and I will look into dependency injection.
            gimbal2 wrote:
            Think about it: you create the MyBean instance yourself. Why do you expect the container to magically inject the EJB reference into it then?

            Use proper JSF mechanics to inject the MyBean1 instance into MyBean2! Any JSF 2 book or article would have told you how. You can use the JSF ManagedProperty annotation:
            @ManagedProperty(value="#{mybean1}")
            private MyBean1 myBean1;
            or if you're on a JEE 6 container you can invest good time into researching how to use CDI to have only one standardized injection model.

            And a final tip: you're making your life unnecessarily difficult by overriding the default bean names.