@EJB injection fails but JNDI lookup works in web

2019-06-25 12:21发布

问题:

I have a @WebService class that injects an @EJB. The EJB is packaged in a .jar file which is in the same .war file as the web service classes. The @EJB injection always fails, but I can do a JNDI lookup on the EJB. I've tried making the EJB and its interface @Remote, but that didn't matter. Injection still fails and JNDI lookup still works.

I'm using a version 3.0 web.xml. There's no ejb deployment descriptor in the ejb.jar file, but that is not suppose to matter in EJB 3.1.

An I missing something or is this a bug in Glassfish?

Here's my code.

EJB class and interface packaged in a .jar in the .war file:

//@Remote
public interface ReportServiceI {

    public String testAlive();
}

@Stateless
//@Remote (ReportServiceI.class)
public class ReportService implements ReportServiceI {...}

Web service class:

@WebService(
  targetNamespace = "http://www.reps.corp.com/services/reports/ReportService", 
  portName="ReportPort",
  serviceName="ReportService", 
  endpointInterface="com.corp.reps.reports.ws.server.ReportServiceWSI")

public class ReportServiceWS implements ReportServiceWSI {

  public static Logger logger = Logger.getLogger(ReportServiceWS.class);

//  These all fail
//  @EJB
//  @EJB(beanInterface=ReportServiceI.class)
//  @EJB(lookup="java:global/repsreports/ReportService")
  ReportServiceI reportService;

  public String testAlive() {

    //  this works
    try {
      InitialContext context = new InitialContext();
      reportService = (ReportServiceI)context.lookup("java:global/repsreports/ReportService");
    }
    catch (NamingException ex) {
      logger.error(ex);

      return "InitialContext.lookup() failed.";
    }

回答1:

This is a bug in Glassfish (apparently in the web services stack).



回答2:

Piotr,

No to both.

beans.xml is a CDI file and I'm using Glassfish as the injection container. I'll try making the @WebService bean @stateless - it's about the only thing I haven't tried - but that shouldn't be necessary.

(This forum software won't allow me to add a comment below Piotr's.)