How to inject EJB in SOAPHandler?

My JAX-WS war contains the 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 is trying to invoke an 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 the look of an EJB.

 @LocalBean @Stateless public class MyEJB { } 

Can someone please tell me how to introduce EJB in SOAPHandlers?

UPDATE / (possibly) ANSWER

I changed @Inject to @EJB and it works.

Is there a way to work with @Inject ? I look IMHO better. :)

+6
source share
3 answers

If I am not mistaken, SOAPHandler is called before the web service is called. According to the CDI specification (see scopes and contexts ) in the context of web services, all normal scopes are active only when the web service is called. In addition to all normal areas, there is also the @Dependent pseudo-area. Unless otherwise specified, this is the default area. This life cycle depends on one of the normal areas and cannot exist by itself.

Now, without saving the state of the EJB, since it does not have CDI-related annotations, it is automatically @Dependent and cannot be entered (using @Inject ) anywhere where the normal area is not active. In your case, there is no active area inside SOAPHandler , so you cannot use @Inject .

Use @EJB , that's okay with that.

+5
source

If you comment on your soap handler class using the CDI "@named" annotation, you can enter any EJB component. This way your soap handler class will become a container-managed bean, and you will start using other container-managed beans like EJB.

 @Named public class MyHandler implements SOAPHandler'<'SOAPMessageContext'>' { .... @Inject private MyEJB myEJB; ... } 
+1
source

All Articles