This discussion is archived
2 Replies Latest reply: May 25, 2012 3:17 AM by RenévanWijk RSS

Cannot inject @EJB in JSF2.0 @ManagedBean on Weblogic 10.3.5

762358 Newbie
Currently Being Moderated
I am using EJB 3.0 and JSF 2.0 (Mojarra) on Weblogic 10.3.5.

In order to be able to inject EJBs in my JSF managed beans, I followed the instructions Re: Netbeans+EJB3.0 , @EJB injection in JSFManagedBean doesn't work. thread, as well as the subsequent [blog post|http://buttso.blogspot.com/2011/03/jsf-with-managed-beans-and-dependency.html] .

I tried every approach described in the blog post:
* Used the JSF implementation as fetched by Maven with the Weblogic dependency injection jar (extracted from Weblogic shared library war).
* Used the Weblogic JSF implementation as a shared library
* Used the Weblogic JSF implementation as an application library (again, extracted shared library war and used the 4 included war files as application libraries).

The @EJB annotation in my Managed Bean is null, and I get a NullPointerException.

I deployed the sample application provided by the blog post, and it works without a problem, which shows that I am doing something wrong.

My application has the following layout:

ear
|-lib
| |- a-lot-of-jars
|- ejb1.jar
|- ejb2.jar
|- ejb3.jar
|- webapp.war

The war is skinny (no jars in the war, all jars including JSF are in ear/lib).

This is the EJB interface:

package eu.ema.eudract.results.participantflow;


import javax.ejb.Local;

@Local
public interface ParticipantFlowService {


This is the EJB implementation:
@Stateless
public class ParticipantFlowServiceBean implements ParticipantFlowService{

This is the managed bean:

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;

@ManagedBean
public class ParticipantFlowBean {
     
     @EJB
     private ParticipantFlowService participantFlowService;


This is my weblogic.xml (in webapp.war):

<?xml version="1.0" encoding="UTF-8"?>
<weblogic-web-app xmlns="http://xmlns.oracle.com/weblogic/weblogic-web-app"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd http://xmlns.oracle.com/weblogic/weblogic-web-app http://xmlns.oracle.com/weblogic/weblogic-web-app/1.0/weblogic-web-app.xsd">

     <container-descriptor>
          <prefer-web-inf-classes>true</prefer-web-inf-classes>
     </container-descriptor>

     <weblogic-version>${weblogic.version}</weblogic-version>

     <context-root>review-web</context-root>

</weblogic-web-app>

This is my web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/j2ee"
     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_5.xsd">
     <display-name>Clinical Trials Results</display-name>
     
     <context-param>
          <param-name>javax.faces.DEFAULT_SUFFIX</param-name>
          <param-value>.xhtml</param-value>
     </context-param>
     <context-param>
          <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
          <param-value>server</param-value>
     </context-param>
     <context-param>
          <param-name>javax.faces.CONFIG_FILES</param-name>
          <param-value>/WEB-INF/application.xml, /WEB-INF/navigation.xml</param-value>
     </context-param>
     <context-param>
          <param-name>javax.faces.FACELETS_REFRESH_PERIOD</param-name>
          <param-value>2</param-value>
     </context-param>
     <context-param>
          <param-name>facelets.DEVELOPMENT</param-name>
          <param-value>${facelets.dev}</param-value>
     </context-param>
     <context-param>
          <param-name>javax.faces.FACELETS_SKIP_COMMENTS</param-name>
          <param-value>true</param-value>
     </context-param>
     <context-param>
          <param-name>facelets.RECREATE_VALUE_EXPRESSION_ON_BUILD_BEFORE_RESTORE</param-name>
          <param-value>false</param-value>
     </context-param>
     <context-param>
          <param-name>com.sun.faces.validateXml</param-name>
          <param-value>true</param-value>
     </context-param>
     <context-param>
          <param-name>com.sun.faces.verifyObjects</param-name>
          <param-value>false</param-value>
     </context-param>
     <context-param>
          <param-name>org.ajax4jsf.COMPRESS_SCRIPT</param-name>
          <param-value>true</param-value>
     </context-param>
     <context-param>
          <param-name>org.ajax4jsf.COMPRESS_STYLE</param-name>
          <param-value>true</param-value>
     </context-param>
     <context-param>
          <param-name>org.ajax4jsf.xmlparser.ORDER</param-name>
          <param-value>TIDY</param-value>
     </context-param>
     <context-param>
          <param-name>org.richfaces.CONTROL_SKINNING</param-name>
          <param-value>enable</param-value>
     </context-param>
     <context-param>
          <param-name>org.ajax4jsf.DEFAULT_EXPIRE</param-name>
          <param-value>86400</param-value>
     </context-param>
     <context-param>
          <param-name>org.richfaces.LoadStyleStrategy</param-name>
          <param-value>ALL</param-value>
     </context-param>
     <context-param>
          <param-name>org.richfaces.LoadScriptStrategy</param-name>
          <param-value>ALL</param-value>
     </context-param>
     <context-param>
          <param-name>com.sun.faces.numberOfViewsInSession</param-name>
          <param-value>20</param-value>
     </context-param>
     <context-param>
          <param-name>com.sun.faces.numberOfLogicalViews</param-name>
          <param-value>20</param-value>
     </context-param>
     
     <context-param>
          <param-name>facelets.LIBRARIES</param-name>
          <param-value>/WEB-INF/eudract.taglib.xml</param-value>
     </context-param>
     
     <context-param>
          <param-name>org.richfaces.SKIN</param-name>
          <param-value>#{skinBean.skin}</param-value>
     </context-param>
     
     <context-param>
     <param-name>javax.faces.DISABLE_FACELET_JSF_VIEWHANDLER</param-name>
     <param-value>true</param-value>
     </context-param>
     
     <context-param>
     <param-name>com.sun.faces.injectionProvider</param-name>
     <param-value>com.bea.faces.WebLogicInjectionProvider</param-value>
     </context-param>
     
     <!-- Filters -->
     
     <filter>
          <display-name>RichFaces Filter</display-name>
          <filter-name>richfaces</filter-name>
          <filter-class>org.ajax4jsf.Filter</filter-class>
          <init-param>
               <description>Set the size limit for uploaded files as attachments in bytes. (max 5MB)</description>
               <param-name>maxRequestSize</param-name>
               <param-value>5242880</param-value>
          </init-param>
     </filter>
     
     <filter-mapping>
          <filter-name>richfaces</filter-name>
          <servlet-name>Faces Servlet</servlet-name>
          <dispatcher>REQUEST</dispatcher>
          <dispatcher>FORWARD</dispatcher>
          <dispatcher>INCLUDE</dispatcher>
     </filter-mapping>
     
     <!-- Listeners -->
     <listener>
          <listener-class>eu.ema.eudract.results.configuration.SystemPropertiesContextListener</listener-class>
     </listener>
     <listener>
          <listener-class>com.sun.faces.config.ConfigureListener</listener-class>
     </listener>
     <listener>
          <listener-class>eu.ema.eudract.results.configuration.ConfigurationServlet</listener-class>
     </listener>
     
     <!-- Faces Servlet -->
     <servlet>
          <servlet-name>Faces Servlet</servlet-name>
          <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
          <load-on-startup>1</load-on-startup>
     </servlet>
     
     <!-- Faces Servlet Mapping -->
     <servlet-mapping>
          <servlet-name>Faces Servlet</servlet-name>
          <url-pattern>*.xhtml</url-pattern>
     </servlet-mapping>
     
     <ejb-local-ref>
          <ejb-ref-name>ejb/ParticipantFlowService</ejb-ref-name>
          <ejb-ref-type>Session</ejb-ref-type>
          <local>eu.ema.eudract.results.participantflow.ParticipantFlowService</local>
     </ejb-local-ref>
     
     <welcome-file-list>
          <welcome-file>index.xhtml</welcome-file>
     </welcome-file-list>

     <mime-mapping>
          <extension>js</extension>
          <mime-type>application/javascript</mime-type>
     </mime-mapping>
     
</web-app>


This is the weblogic-application.xml:

<?xml version='1.0'?>
<!--Generated by Oracle SmartUpgrade (11.1.1.2.0)-->
<weblogic-application xmlns="http://xmlns.oracle.com/weblogic/weblogic-application"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://xmlns.oracle.com/weblogic/weblogic-application http://xmlns.oracle.com/weblogic/weblogic-application/1.0/weblogic-application.xsd">

<module>
<name>${datasource.name}DS</name>
<type>JDBC</type>
<path>data-sources-jdbc.xml</path>
</module>

<prefer-application-packages>
     <package-name>antlr.*</package-name>
     <package-name>antlr.collections.*</package-name>
     <package-name>antlr.collections.impl.*</package-name>
     <package-name>antlr.debug.misc.*</package-name>
     <package-name>com.sun.el.*</package-name>
     <package-name>org.apache.commons.*</package-name>
     <package-name>org.apache.xerces.*</package-name>
     <package-name>javax.faces.*</package-name>
     <package-name>com.sun.faces.*</package-name>
     </prefer-application-packages>

     
</weblogic-application>


This is the weblogic-ejb-jar.xml:

<?xml version="1.0" encoding="UTF-8"?>
<weblogic-ejb-jar xmlns="http://www.bea.com/ns/weblogic/90"/>

Edited by: deadlock_gr on Jun 13, 2012 6:02 AM
  • 1. Re: Cannot inject @EJB in JSF2.0 @ManagedBean on Weblogic 10.3.5
    164415 Oracle ACE
    Currently Being Moderated
    Hi -- thanks for the well constructed post, makes it easier to follow what you are doing and have configured.

    Taking it back to the first principle, can you again try using the simplest approach which is to deploy the jsf-2.0.war shared-library, then reference that from your application.

    I'm currently checking on what debug options/flags exist to try and get more information from the execution call to try and identify where any problems are occurring.

    -steve-
  • 2. Re: Cannot inject @EJB in JSF2.0 @ManagedBean on Weblogic 10.3.5
    RenévanWijk Oracle ACE
    Currently Being Moderated
    Can you try with deleting the local ref,
    <ejb-local-ref>
    <ejb-ref-name>ejb/ParticipantFlowService</ejb-ref-name>
    <ejb-ref-type>Session</ejb-ref-type>
    <local>eu.ema.eudract.results.participantflow.ParticipantFlowService</local>
    </ejb-local-ref>
    As you are using @EJB to inject a reference to the EJB, the container automatically binds it to java:comp/env/package-name.ParticipantFlowBean/ParticipantFlowService.

    Maybe, not sure about this, the local-ref definition disturbs the look-up, which is performed by the container itself when you are using @EJB.

    An example, can be found here (http://middlewaremagic.com/weblogic/?p=7716). Note that here a servlet is used that references the EJB.
    Here the EJB is bound as:
    @Stateless(name = "Company", mappedName = "ejb/Company")
    public class CompanyBean implements Company {...}
    and the servlet references it as follows:
    @WebServlet(name = "TestServlet", urlPatterns = "/testservlet")
    public class TestServlet extends HttpServlet {
        @EJB(name = "Company", mappedName = "ejb/Company")
        Company company;
    }
    without any local-ref etcetera configured.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points