How to inject EJB into SOAPHandler?

本小妞迷上赌 提交于 2019-12-10 13:39:18

问题


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. :)


回答1:


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.




回答2:


If you annotate your soap handler class with CDI "@named" annotation, you can inject any EJB component. By doing so, your soap handler class becomes a container managed bean and you start to be able to use other container managed beans as EJBs.

@Named

public class MyHandler implements SOAPHandler'<'SOAPMessageContext'>' {

....

@Inject

private MyEJB myEJB; 

...

}



回答3:


In case someone else google here like I did, it's possible to do that in TomEE: http://svn.apache.org/repos/asf/tomee/tomee/trunk/server/openejb-cxf/src/test/java/org/apache/openejb/server/cxf/handler/SimpleHandler.java



来源:https://stackoverflow.com/questions/14099350/how-to-inject-ejb-into-soaphandler

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!