gpt4 book ai didi

java - 如何在 SOAPHandler 中使用 spring bean

转载 作者:行者123 更新时间:2023-11-30 07:41:50 25 4
gpt4 key购买 nike

我只想在 handler-chain.xml 中调用的 handleMessage() 方法中使用 spring bean。

这是我的 handler-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;
}

这是我的 spring 配置 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 为 null。如何在handleMessage方法中使用serviceInvokeUtil类作为spring bean?

最佳答案

我将 SpringBeanAutowiringSupport 扩展到 CustomerHandler 类,然后它就解决了。像这样: public class CustomerHandler extends SpringBeanAutowiringSupport Implements SOAPHandler {.....

关于java - 如何在 SOAPHandler 中使用 spring bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34559059/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com