2016-01-01 19 views
1

使用一個Spring bean我只是想handleMessage()方法被稱爲在處理-chain.xml中使用一個Spring bean。如何內SOAPHandler

這是我的處理程序,chain.xml:

<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> 

而且也這是我CustomerHandler.java類:

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; 
} 

這是我的彈簧配置xml:

<?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> 

當我打電話給web服務時,spring bean serviceInvokeUtil爲空。 如何使用serviceInvokeUtil類爲handleMessage方法中的彈簧豆?

回答

1

我伸出SpringBeanAutowiringSupport到CustomerHandler類比它解決了。像這樣的:公共類CustomerHandler擴展SpringBeanAutowiringSupport實現SOAPHandler {.....

0

Spring將過程@Autowired註解,只有當它會控制實例的創建。換句話說,你的CustomerHandler必須是bean以及ServiceInvokeUtil來強制spring自動裝入它。

+0

首先,感謝喲對relpy。我已經試過了。我將SpringHelper類作爲spring bean放入spring配置xml中。像這樣:但結果是一樣的。你有什麼不同的想法? – kozmo

相關問題