---
title: "axis2+spring集成"
author: "ywChen"
date: "2013-06-04T14:12:22Z"
tags: []
categories: ["企业架构"]
slug: "axis2-spring"
draft: false
lastmod: "2013-06-04T14:12:22Z"
id: 1882037
url: "https://ywchen.iteye.com/blog/1882037"
---

# [axis2+spring集成](http://www.cnblogs.com/linjiqin/archive/2011/07/05/2098316.html)

1、新建一个web project项目，最终工程目录如下：

![](http://pic002.cnblogs.com/images/2011/270324/2011070515075348.png)

注意：本文只注重webservice服务器端的开发，因此com.ljq.client和com.ljq.test忽略不计

         

2、添加所需jar

![](http://pic002.cnblogs.com/images/2011/270324/2011070515104715.png)

            

3、接口HelloWorld

package com.ljq.service;

publicinterface HelloWorld {  
public String greeting(String name);  
public String print();  
}

         

4、接口实现类HelloWorldBean

[![复制代码](http://common.cnblogs.com/images/copycode.gif)]()

package com.ljq.service;

publicclass HelloWorldBean implements HelloWorld {  
public String greeting(String name) {  
return"你好 "+name;  
}

public String print() {  
return"我叫林计钦";  
}  
}

[![复制代码](http://common.cnblogs.com/images/copycode.gif)]()

          

5、webservice类HelloWorldWebService

[![复制代码](http://common.cnblogs.com/images/copycode.gif)]()

package com.ljq.service;

import org.apache.axis2.AxisFault;  
import org.apache.axis2.ServiceObjectSupplier;  
import org.apache.axis2.description.AxisService;  
import org.apache.axis2.description.Parameter;  
import org.apache.axis2.i18n.Messages;  
import org.springframework.beans.BeansException;  
import org.springframework.context.ApplicationContext;  
import org.springframework.context.ApplicationContextAware;

/\**  
\* 可能出现Axis2 spring bean not found 或者 Spring applicationContext not found。  
\*  
\* 解决办法：构建自己的ServiceObjectSupplier，实现接口ServiceObjectSupplier，同时也实现Spring的ApplicationContextAware接口  
\*  
\*  
\* @author Administrator  
\*  
\*/  
publicclass HelloWorldWebService implements ServiceObjectSupplier,  
ApplicationContextAware {  
privatestatic ApplicationContext ctx;

public Object getServiceObject(AxisService axisService) throws AxisFault {  
Parameter springBeanName = axisService.getParameter("SpringBeanName");  
String beanName = ((String) springBeanName.getValue()).trim();  
if (beanName !=null) {  
if (ctx ==null)  
thrownew AxisFault("applicationContext is NULL! ");  
if (ctx.getBean(beanName) ==null)  
thrownew AxisFault("Axis2 Can't find Spring Bean: "+ beanName);  
return ctx.getBean(beanName);  
} else {  
thrownew AxisFault(Messages.getMessage("paramIsNotSpecified",  
"SERVICE\_SPRING\_BEANNAME"));  
}

}

publicvoid setApplicationContext(ApplicationContext ctx)  
throws BeansException {  
this.ctx = ctx;  
}

}

[![复制代码](http://common.cnblogs.com/images/copycode.gif)]()

            

6、配置web.xml文件

[![复制代码](http://common.cnblogs.com/images/copycode.gif)]()

&lt;?xml version="1.0" encoding="UTF-8"?&gt;  
&lt;web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"  
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee  
http://java.sun.com/xml/ns/javaee/web-app\_2\_5.xsd"&gt;  
&lt;!-- 添加spring监听器 --&gt;  
&lt;listener&gt;  
&lt;listener-class&gt;  
org.springframework.web.context.ContextLoaderListener  
&lt;/listener-class&gt;  
&lt;/listener&gt;  
&lt;!-- 加载spring的配置文件 --&gt;  
&lt;context-param&gt;  
&lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;  
&lt;param-value&gt;/WEB-INF/applicationContext.xml&lt;/param-value&gt;  
&lt;/context-param&gt;

&lt;!-- 注册axis2的servlet --&gt;  
&lt;servlet&gt;  
&lt;servlet-name&gt;AxisServlet&lt;/servlet-name&gt;  
&lt;servlet-class&gt;  
org.apache.axis2.transport.http.AxisServlet  
&lt;/servlet-class&gt;  
&lt;load-on-startup&gt;1&lt;/load-on-startup&gt;  
&lt;/servlet&gt;  
&lt;servlet-mapping&gt;  
&lt;servlet-name&gt;AxisServlet&lt;/servlet-name&gt;  
&lt;url-pattern&gt;/services/\*&lt;/url-pattern&gt;  
&lt;/servlet-mapping&gt;

&lt;welcome-file-list&gt;  
&lt;welcome-file&gt;index.jsp&lt;/welcome-file&gt;  
&lt;/welcome-file-list&gt;  
&lt;/web-app&gt;

[![复制代码](http://common.cnblogs.com/images/copycode.gif)]()

       

7、在WEB-INF目录下配置applicationContext.xml（不存在则自己创建）

[![复制代码](http://common.cnblogs.com/images/copycode.gif)]()

&lt;?xml version="1.0" encoding="UTF-8"?&gt;  
&lt;!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"  
"http://www.springframework.org/dtd/spring-beans.dtd"&gt;  
&lt;beans&gt;  
&lt;bean id="applicationContext"  
class="org.apache.axis2.extensions.spring.receivers.ApplicationContextHolder"/&gt;  
&lt;bean id="helloWorld" class="com.ljq.service.HelloWorldBean"&gt;&lt;/bean&gt;  
&lt;/beans&gt;

[![复制代码](http://common.cnblogs.com/images/copycode.gif)]()

         

8、在WEB-INF\\services\\axis\\META-INF\\目录下配置services.xml（不存在则自己创建）

[![复制代码](http://common.cnblogs.com/images/copycode.gif)]()

&lt;?xml version="1.0" encoding="UTF-8"?&gt;  
&lt;service name="hwWebService"&gt;  
&lt;description&gt;axis2与spring集成案例&lt;/description&gt;  
&lt;!-- 通过ServiceObjectSupplier参数指定SpringServletContextObjectSupplier类来获得Spring的ApplicationContext对象 --&gt;  
&lt;parameter name="ServiceObjectSupplier"&gt;  
org.apache.axis2.extensions.spring.receivers.SpringAppContextAwareObjectSupplier  
&lt;/parameter&gt;  
&lt;!--  
SpringBeanName固定的不能改  
helloWorld是spring中注册的实现类得id  
\--&gt;  
&lt;parameter name="SpringBeanName"&gt;helloWorld&lt;/parameter&gt;  
&lt;!--  
在这里最值得注意的是&lt;messageReceivers&gt;元素，该元素用于设置处理WebService方法的处理器。  
例如，getGreeting方法有一个返回值，因此，需要使用可处理输入输出的RPCMessageReceiver类，  
而update方法没有返回值，因此，需要使用只能处理输入的RPCInOnlyMessageReceiver类。  
\--&gt;  
&lt;messageReceivers&gt;  
&lt;messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-only"  
class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver"/&gt;  
&lt;messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out"  
class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/&gt;  
&lt;/messageReceivers&gt;  
&lt;/service&gt;

[![复制代码](http://common.cnblogs.com/images/copycode.gif)]()

        

axis2+spring集成到此已经开发完成，接下把工程部署到tomcat，

然后通过[http://localhost:8083/axis2spring/services/hwWebService?wsdl](http://localhost:8083/axis2spring/services/hwWebService?wsdl)访问