axis2.1.6+spring集成

1、首先你的有一个Spring框架的工程,Spring的工程网上有很多,我用的ssh的框架。

2添加所需jar


客服端最基本的架包,再加上:

woden-api-1.0M9.jar
jsr311-api-1.0.jar
axis2-spring-1.6.2.jar

3、Dao类

WebDao接口类

package wip;

public interface WebDao{
	public String getTest(String testName);
}

WebDao实体类

package wip;

public class WebDaoImpl implements WebDao{
	public String getTest(String testName){
		return "This is Test:" + testName;
	}
}

4、Service类

WebService接口类

package wip;

public interface WebService{
	public String getTest(String testName);
}
WebService实体类
package wip;

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;

/**
 * 解决: Spring applicationContext not found。
 * 需要实现接口ServiceObjectSupplier,同时也实现Spring的ApplicationContextAware接口
 */
public class WebServiceImpl implements WebService,ServiceObjectSupplier,ApplicationContextAware{
	
	private static 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)
                throw new AxisFault("applicationContext is NULL! ");
            if (ctx.getBean(beanName) == null)
                throw new AxisFault("Axis2 Can't find Spring Bean: " + beanName);
            return ctx.getBean(beanName);
        } else {
            throw new AxisFault(Messages.getMessage("paramIsNotSpecified",
                    "SERVICE_SPRING_BEANNAME"));
        }

    }

    public void setApplicationContext(ApplicationContext ctx)
            throws BeansException {
        this.ctx = ctx;
    }
	
    private WebDao webDao;
	public WebDao getWebDao() {
		return webDao;
	}
	public void setWebDao(WebDao webDao) {
		this.webDao = webDao;
	}

	public String getTest(String testName){
		return webDao.getTest(testName);
	}
}

5、配置web.xml文件

<!-- spring的监听器,以便在启动时就自动加载spring的配置 --> 
    <listener>  
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
    </listener> 
    <!-- spring要加载的配置文件-->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:/applicationContext*.xml</param-value>
	</context-param>
    
    <!-- 注册axis2的servlet -->
    <servlet>
        <servlet-name>AxisServlet</servlet-name>
        <servlet-class>org.apache.axis2.transport.http.AxisServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>AxisServlet</servlet-name>
        <url-pattern>/services/*</url-pattern>
    </servlet-mapping>

6、配置applicationContext-spring-bean.xml文件

<bean id="applicationContext"
        class="org.apache.axis2.extensions.spring.receivers.ApplicationContextHolder" />
	<bean id="webService" class="wip.WebServiceImpl">
		<property name="webDao" ref="webDao"></property> 
	</bean>
	<bean id="webDao" class="wip.WebDaoImpl">
	</bean>



7、在WEB-INF\services\webService\META-INF\services.xml目录下配置(新建)

<?xml version="1.0" encoding="utf-8"?>
<service name="webService" targetNamespace="http://wip.webServices"> 
  <description><b>webService接口</b></description>
  <schema schemaNamespace="http://wip.webServices/webService"/>
  <!-- 接口类设置 -->
  <parameter name="ServiceClass">wip.WebService</parameter>
  <!-- 通过ServiceObjectSupplier参数指定SpringServletContextObjectSupplier类来获得Spring的ApplicationContext对象 -->
  <parameter name="ServiceObjectSupplier">
      org.apache.axis2.extensions.spring.receivers.SpringAppContextAwareObjectSupplier
  </parameter>
  <!--  SpringBeanName固定的不能改  helloWorld是spring中注册的实现类得id -->
  <parameter name="SpringBeanName">webService</parameter>
  <operation name="getTest"></operation> 
  <!-- 在这里最值得注意的是<messageReceivers>元素,该元素用于设置处理WebService方法的处理器。
    	例如,get方法有一个返回值,因此,需要使用可处理输入输出的RPCMessageReceiver类,
   		而set方法没有返回值,因此,需要使用只能处理输入的RPCInOnlyMessageReceiver类。 -->
  <messageReceivers>
    <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out"
        class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
  </messageReceivers>
</service>



8、在Web工程->WebRoot目录下新建axis2-web -> listServices.jsp目录和文件

<%@ 
page contentType="text/html;charset=UTF-8" language="java" 
%><%@ 
page import="org.apache.axis2.Constants, 
            org.apache.axis2.description.AxisOperation, 
            org.apache.axis2.description.AxisService, 
            java.util.Collection, 
            java.util.HashMap, 
            java.util.Iterator" 
%><html> 
<head><title>List Services</title> 
<style> 
h2{margin:20 0 5 0;} 
ul{margin-top:5;} 
</style> 
</head> 
<body> 
<h1>Available services</h1> 
<% 
    HashMap serviceMap = (HashMap) request.getSession().getAttribute(Constants.SERVICE_MAP); 
    Collection servicecol = serviceMap.values(); 
    if(servicecol.size()==0){%>Available services is Empty.<%} 
    for (Iterator iterator = servicecol.iterator(); iterator.hasNext();) { 
        AxisService axisService = (AxisService) iterator.next(); 
        Iterator opItr = axisService.getOperations(); 
        String serviceName = axisService.getName(); 

%> 

<h2><font color="blue"><a href="<%=serviceName %>?wsdl" target="_blank"><%=serviceName%></a></font></h2> 
<i>Available Operations</i> 
<ul> 
<% 
while (opItr.hasNext()) { 
    AxisOperation axisOperation = (AxisOperation) opItr.next(); 
    %><li><%=axisOperation.getName().getLocalPart()%></li><% 
} 
%> 
</ul> 

<% 
    } 
%> 
</body> 
</html> 


9、发布我们新建的Web工程 

打开浏览器访问:http://localhost:8080/WebService//services/listServices 
会出现我们部署的服务  webService 

测试可以参照:http://blog.csdn.net/huanglgln/article/details/41648877

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值