5 Replies Latest reply: Oct 5, 2011 9:08 PM by Steve Button-Oracle RSS

    EJB Injection

    HDGeek
      I am trying to use EJB injection from a managed bean (ManagedBean2), that is called from another managed bean (ManagedBean1), that is called directly from my .jspx page. I get a null pointer back and cannot access my method in the EJB Session facade. If I use EJB injection in ManagedBean1, it works fine. Any suggestions?

      Example:
      page.jspx
      <af:tree value="ManagedBean1.method"..../>

      faces-config.xml
      <managed-bean>
      <managed-bean-name>ManagedBean1</managed-bean-name>
      <managed-bean-class>ManagedBean1</managed-bean-class>
      <managed-bean-scope>session</managed-bean-scope>
      <managed-property>
      <property-name>myproperty</property-name>
      <value>#{ManagedBean2.method}</value>
      </managed-property>
      </managed-bean>
      <managed-bean>
      <managed-bean-name>ManagedBean2</managed-bean-name>
      <managed-bean-class>ManagedBean2</managed-bean-class>
      <managed-bean-scope>session</managed-bean-scope>
      </managed-bean>

      ManagedBean2.java
      public class ManagedBean2 {

      @EJB
      private SessionFacadeLocal local;

      //the constructor
      public ManagedBean2() throws Exception {

      List<someList> list = local.method(); //BOMBS HERE! NULL POINTER EXCEPTION!

      .....more code....

      }
      }
        • 1. Re: EJB Injection
          Csoto-Oracle
          Hi HDGeek,

          Are you using JEE 6 or later? I ask you this because you may need JSR-299 and JSR-330 for this.

          A workaround to '@EJB' injection not working is to use lookup the EJB through JNDI instead. To lookup through a @Remote interface, you can do the JNDI lookup in the constructor, like this:

          public MyBackingBean() {
          /* Comment this block out to test whether @EJB injection is working */
          InitialContext ic;
          try {
          ic = new InitialContext();
          xxx = (SessionEJB)ic.lookup("SessionEJB");
          } catch (NamingException e) {
          System.out.println("Error " + e.getMessage());
          e.printStackTrace();
          }
          /* */
          }

          Hope it helps.

          -Cris
          • 2. Re: EJB Injection
            HDGeek
            Yes, we're using JEE6. We're developing in JDeveloper 11.1.1.5, with Weblogic 10.3.5. I did try using the lookup also, but did not have luck with that either, as I couldn't get it to find my sessionEJB.
            • 3. Re: EJB Injection
              Olaf Heimburger-Oracle
              You can inspect the JNDI tree of WLS by opening the WLS console, selecting Servers, selecting ServerName, and clicking on the JNDI browser link.
              You will find the Session EJB by name with a FQCN as a sub directory structure, use this as the lookup string (eg. MySessionEJB#demo.ejb.MySessionFacade).

              HTH,
              --olaf
              PS: I know injection should work out of the box, but facing a similar issue on some configurations, but haven't found a general solution yet...
              • 4. Re: EJB Injection
                Steve Button-Oracle
                Yes, we're using JEE6. We're developing in JDeveloper 11.1.1.5, with Weblogic 10.3.5.
                Actually if you are using JDeveloper 11.1.1.5 and WebLogic 10.3.5 then you are not using Java EE 6.

                WebLogic Server 10.3.5 is Java EE 5 based, with optional support for JSF 2.0, JPA 2.0 and JAX-RS 1.1. We have Java EE 6 coming in the next major release of WebLogic Server.

                But with that said, you can @EJB to inject an EJB 3.0 bean into a JSF 2.0 ManagedBean.

                What you need to ensure you do is to use the WebLogic Server bundled JSF 2.0 shared-library as the JSF implementation. The short story is that the class we implement to handle the dependency injection is bundled within the jsf-2.0.war shared-library, that we provide to support JSF 2.0. Thus in order for DI to work on WLS, you need to use that library. See the longer story here: http://buttso.blogspot.com/2011/03/jsf-with-managed-beans-and-dependency.html.

                I'm not entirely sure how you are doing it, nor how JDeveloper manages these libraries/dependencies if you are relying on it. But I'd certainly suggest having a poke around since using @ManagedBean and @EJB for DI does work on WLS 10.3.5.

                cheers
                -steve-
                • 5. Re: EJB Injection
                  Steve Button-Oracle
                  Olaf, take a look at the blog posting, hopefully that helps answer the question about why DI may not be working:

                  http://buttso.blogspot.com/2011/03/jsf-with-managed-beans-and-dependency.html

                  It's not documented and we don't test it, but I have successfully demonstrated pulling the wls.jsf.di.jar library out of the jsf-2.0.war, installing it as a Maven artifact, and then using it as dependency (packaged) in conjunction with the stock Mojarra 2.0 distribution and using DI with EJBs in an application.

                  The implementation we provide of the com.sun.faces.spi.InjectionProvider interface is very simple and essentially acts as a callback into WLS to handle the JSF injection events.

                  -steve-