Search code examples
ejbjax-wscdi

How to inject EJB into SOAPHandler?


My JAX-WS war contains following entries.

WEB-INF/lib/
WEB-INF/beans.xml // empty
WEB-INF/lib/commons-logging-1.1.1.jar
WEB-INF/lib/corrs-beans-1.0-alpha-1-SNAPSHOT.jar // EJBs are here
WEB-INF/lib/corrs-entities-1.0-alpha-1-SNAPSHOT.jar
WEB-INF/lib/joda-time-1.6.2.jar
WEB-INF/lib/opensaml-2.5.1-1.jar
WEB-INF/lib/openws-1.4.2-1.jar
WEB-INF/lib/slf4j-api-1.6.1.jar
WEB-INF/lib/wss4j-1.6.8.jar
WEB-INF/lib/xmlsec-1.5.3.jar
WEB-INF/lib/xmltooling-1.3.2-1.jar
WEB-INF/web.xml
META-INF/maven/
META-INF/maven/kr.co.ticomms.corrs/
META-INF/maven/kr.co.ticomms.corrs/corrs-services/
META-INF/maven/kr.co.ticomms.corrs/corrs-services/pom.xml
META-INF/maven/kr.co.ticomms.corrs/corrs-services/pom.properties

One of my SOAPHandlers trying to call EJB.

@HandlerChain(file=...)
@WebService(...)
public class MyService {
}

public class MyHandler implements SOAPHandler<SOAPMessageContext> {

    @Override
    public boolean handleMessage(final SOAPMessageContext context) {
        // MyEJB null
    }

    @Inject
    private MyEJB myEJB; // << NULL
}

MyEJB is just an nointerface-view EJB.

@LocalBean
@Stateless
public class MyEJB {
}

Can anybody please tell me how to inject EJBs into SOAPHandlers?

UPDATE / (maybe)ANSWER

I changed @Inject to @EJB and it works.

Is there any way to work with @Inject? I looks IMHO better. :)


Solution

  • If I'm not mistaken, SOAPHandlers are invoked before the web service invocation. According to the CDI spec (see scopes and contexts), in a web services context all the normal scopes are only active during the web service invocation. In addition to all the normal scopes, there is also the @Dependent pseudo scope. Unless otherwise specified, this is the default scope. It's life cycle depends on one of the normal scopes and as such cannot exist by itself.

    Now, a stateless EJB since it has no CDI related annotations, it is automatically @Dependent and cannot be injected (using @Inject) anywhere a normal scope is not active. In your case, inside a SOAPHandler there is no scope active so you cannot use @Inject.

    Use @EJB, nothing wrong with that.