读书人

基于jax-ws webservice开发中spring与

发布时间: 2012-10-31 14:37:32 作者: rapoo

基于jax-ws webservice开发中spring与axis2整合问题

最近做一个联通彩信接入的上行模块开发时碰到的问题。

需求描述:开发一个webservice接收来自彩信中心的消息(MO)

使用框架:spring2.5.3 + axis2 1.4.1 (jax-ws2.0) + mina

问题描述:由于是使用jax-ws技术因此根据WSDL生成的代码为annotation的,因此无需配置即可在axis2中发布,所以问题出现:

1、想通过配置来对象注入到service中无法实现(在此时没找到解决此问题的部分)。

2、在serivce中使用axis2 api读取soap消息,消息始终为null。

?

(如果不使用annotation来描述webservice,是可以使用配置来注入bean的,在server端也能正常的读取soap消息。)

?

解决办法:(既然是使用annotation来描述webservice,那当然得用spring annotation来注入了。读取soap消息也一样)

1、用@Autowired来修饰需注入的bean,目标类需继承SpringBeanAutowiringSupport(这个功能应该是在2.5.3的版本中增加的)。

2、使用@Resource来修饰需要捕获的soap消息。

?

代码:

import java.util.Map;import javax.annotation.Resource;import javax.jws.WebMethod;import javax.jws.WebService;import javax.servlet.http.HttpServletRequest;import javax.xml.ws.WebServiceContext;import javax.xml.ws.handler.MessageContext;import org.apache.log4j.Logger;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.context.support.SpringBeanAutowiringSupport;@WebService(targetNamespace = "http://www.demon.service",serviceName = "HelloService", name = "demon")public class Hello extends SpringBeanAutowiringSupport{    /**     * Logger for this class     */    private static final Logger logger = Logger.getLogger(Hello.class);    @Autowired    private XXBean bean;//此处的bean的名称要与spring中bean的id一致。          @Resource    WebServiceContext wsContext;       @WebMethod(operationName = "sayHello")    public String sayHello(String name){    javax.xml.ws.handler.MessageContext context = wsContext.getMessageContext();    }}

参考资料:http://docs.huihoo.com/spring/2.5.x/zh-cn/remoting.html

https://jax-ws.dev.java.net/articles/MessageContext.html

1 楼 liberD 2010-11-19 把例子写的更全面一些吧。O(∩_∩)O哈哈~ 2 楼 stevendu 2011-12-26 bean注入失败,始终为null。

读书人网 >Web前端

热点推荐