Spring mvc with axis (Axis2 Can't find Spring's ApplicationContext)

最近项目中会使用到webservice,实现整个项目中的一个功能,项目使用的是spring mvc + hibernate(annotation),在网上找了一些资料之后,结合项目组的具体情况,决定使用axis2 v1.5.5(网上资料非常多)。为了节省开发成本,打算将axis2整合到项中。

  首先,建一个web项目,将。spring mvc及hibernate (annotation)中要便用的jar包,都拷贝到WEB-INF下lib中,按照平时开发项目的习惯,将准备工作做好。

  然后在apache官网上下载axis2-1.5.5的压缩包,解压之后将lib下的所有jar包都拷贝到WEB-INF下的lib中。

  下面是关键,如果操作不当,配置就不会成功

  1、在webapp下新建axis2-web的文件夹,在这个文件夹中新建一个listServices.jsp,用于迭代项目中的webservice服务,代码如下:

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

 2、配置spring的applicationContext.xml文件, 这里介绍的是以servletContext加载spring配置文件的模式;applicationContext中的配置可以按照之前普通spring bean的配置方式,但是需要在配置文件中加上下面这段  :

 写道
<bean id="applicationContext" class="org.apache.axis2.extensions.spring.receivers.ApplicationContextHolder" />

 3、axis2与spring整合,访问wsdl是没有问题,但是在调用具体服务的时候,会报Axis2 Can't find Spring's ApplicationContext的bub,需要在项目中添加这样的一个类:

 

package com.tiilii.tiisso.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.extensions.spring.receivers.ApplicationContextHolder;
import org.apache.axis2.i18n.Messages;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class SSOWebService implements ServiceObjectSupplier,
		ApplicationContextAware {

	private ApplicationContext applicationContext;

	public Object getServiceObject(AxisService axisService) throws AxisFault {
		Parameter springBeanName = axisService.getParameter("SpringBeanName");
		String beanName = ((String) springBeanName.getValue()).trim();

		if (beanName != null) {
			//这块是个人添加的,在网络上找到很多帖子,不知道如何注入applicationContext,就想了这样的一个笨办法
			applicationContext = ApplicationContextHolder.getContext();
			if (applicationContext == null)
				throw new AxisFault("applicationContext is NULL! ");
			if (applicationContext.getBean(beanName) == null)
				throw new AxisFault("Axis2 Can't find Spring Bean: " + beanName);
			return applicationContext.getBean(beanName);
		} else {
			throw new AxisFault(Messages.getMessage("paramIsNotSpecified",
					"SERVICE_SPRING_BEANNAME"));
		}

	}

	@Override
	public void setApplicationContext(ApplicationContext ctx)
			throws BeansException {
		// TODO Auto-generated method stub
		this.applicationContext = ctx;

	}
}
 

 

4、在WEB-INF下新建一个services的文件夹,将version-1.5.aar拷贝到这个文件夹中;在这个文件夹下新建一个与webservice服务名相关的文件夹(任意命名,只试过英文命名),在这个文件夹下,再新建一个文件夹,命名为META-INF,在这个文件夹下新建services.xml文件,文件内容如下:

 

<?xml version="1.0" encoding="UTF-8"?>
<serviceGroup>
	<service name="TiiSSO">
		<description>WeatherService:Spring POJO Axis2 Service Sample
		</description>
		<parameter name="ServiceClass">
			com.tiilii.tiisso.service.SSOService
		</parameter>
		<parameter name="ServiceObjectSupplier" locked="false">
			com.tiilii.tiisso.service.SSOWebService
		</parameter>
		<parameter name="SpringBeanName" locked="false">SSOService
		</parameter>
		<messageReceivers>
			<messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-only"
				class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" />
			<messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out"
				class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
		</messageReceivers>
	</service>
</serviceGroup> 

 5、最后配置web.xml 

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 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_3_0.xsd">
	<display-name></display-name>

	<context-param>
		<param-name>log4jConfigLocation</param-name>
		<param-value>/WEB-INF/config/log4j.properties</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
	</listener>

	<servlet>
		<servlet-name>TIIWS</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<!-- 如有多个,请以逗号隔开 -->
			<param-value>/WEB-INF/config/applicationContext.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet>
		<display-name>Apache-Axis Servlet</display-name>
		<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>
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>

 由于项目原因,就不上传代码附件了,第一次写帖子,如帖子中有不到的地方,欢迎指正,大家一起交流,共同进步。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值