如何在SOAPHandler中使用spring bean

时间:2016-01-01 18:59:17

标签: java spring web-services jax-ws autowired

我只想在{-1}}方法中使用spring bean,该方法在handler-chain.xml中调用。

这是我的handler-chain.xml:

handleMessage()

这也是我的CustomerHandler.java类:

<jaxws:bindings>
    <handler-chains xmlns="http://java.sun.com/xml/ns/javaee">
        <!-- ====================== -->
        <!-- service based handlers -->
        <!-- ====================== -->
        <handler-chain>
            <handler>
                <handler-name>CustomerHandler</handler-name>
                <handler-class>com.test.ws.handler.CustomerHandler</handler-class>
            </handler>
        </handler-chain>
    </handler-chains>
</jaxws:bindings>

这是我的spring配置xml:

public class CustomerHandler implements SOAPHandler<SOAPMessageContext> {

@Autowired
public ServiceInvokeUtil serviceInvokeUtil;

public Set<QName> getHeaders() {

    return null;
}

public void close(MessageContext context) {

}

public boolean handleFault(SOAPMessageContext context) {
    logSoapMessage(context);
    return false;
}

public boolean handleMessage(SOAPMessageContext context) {


    Boolean isRequest = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
    SOAPMessage soapMsg = context.getMessage();
    // if this is a request, true for outbound messages, false for inbound
    if (isRequest) {

        try {
            SOAPEnvelope soapEnv = soapMsg.getSOAPPart().getEnvelope();
            SOAPHeader soapHeader = soapEnv.getHeader();

            // if no header, add one
            if (soapHeader == null) {
                soapHeader = soapEnv.addHeader();
            }
            Boolean oa = serviceInvokeUtil.createRecord(SoapUtil.getRecordEntity());

            SOAPElement userContextHeader = ConsumerHeaderHelper.createUserContextHeader(context);
            soapHeader.addChildElement(userContextHeader);
            }
            soapMsg.saveChanges();




        } catch (SOAPException e) {
            System.err.println(e);
        }

    } else {

        try {
            SoapUtil.setSoapResponseHeader(soapMsg.getSOAPHeader());
        } catch (SOAPException e) {
            e.printStackTrace();
        }

    }
    logSoapMessage(context);
    return true;
}

当我调用Web服务时,spring bean <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ehcache="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.1.xsd"> <context:property-placeholder /> <bean id="serviceInvokeUtil" class="com.test.ws.util.ServiceInvokeUtil"> </bean> 为null。 如何在serviceInvokeUtil方法中使用serviceInvokeUtil类作为spring bean?

2 个答案:

答案 0 :(得分:1)

我将SpringBeanAutowiringSupport扩展到CustomerHandler类,而不是解决它。像这样:公共类CustomerHandler扩展SpringBeanAutowiringSupport实现SOAPHandler {.....

答案 1 :(得分:0)

只有当它控制实例的创建时,Spring才会处理@Autowired注释。换句话说,你的CustomerHandler必须是bean以及ServiceInvokeUtil来强制spring自动装配它。