Axis2整合spring

来源:互联网 发布:央视网络春晚策划方案 编辑:程序博客网 时间:2024/06/07 03:39

我系统的版本:jdk1.4、eclicpe 3.2、axis2 1.3、 spring1.28


首先web项目新建好,这里指的是spring已经整合好了 只需要把axis2整合进来

第一步:

把axis2所有的包引进到web工程

第二步:

在WEB-INF文件夹下新建services文件夹再新建axis文件夹再新建META-INF文件夹再新建services.xml 如下图:



services.xml文件内容如下:

<serviceGroup>
<service name="IConditionInfoService">
<description>
ConditionInfo Spring POJO Axis2 AAR deployment
</description><!--
<parameter name="ServiceClass">
com.dlmu.database.conditionInfo.service.IConditionInfoService
</parameter>
-->
<parameter name="ServiceObjectSupplier">
org.apache.axis2.extensions.spring.receivers.SpringAppContextAwareObjectSupplier
</parameter>
<parameter name="SpringBeanName">
conditionInfo:conditionInfoService
</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>

这里的conditionInfo:conditionInfoService这个ID是你spring已经实例化好的 我的不在applicationContext.xml 你的可以再这个文件中也可以不在  只要你spring实例化了它就行

applicationContext.xml文件内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- 整合Axis2 -->
<bean id="applicationContext"
       class="org.apache.axis2.extensions.spring.receivers.ApplicationContextHolder" />
</beans>

注意:上面那个bean是一定要的

然后OK了

客户端代码如下:


package com.dlmu.database.client;


import javax.xml.namespace.QName;


import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;


public class Axis2Client {
/**
* @param args
* @author Administrator
*/
public static void main(String[] args) {
/** RPC的实现 整合spring */
xfireTest.testClientByRPC();
}
public void testClientByRPC() {
try {
RPCServiceClient serviceClient = new RPCServiceClient();
Options options = serviceClient.getOptions();
EndpointReference targetEPR = new EndpointReference(
"http://localhost:8080/AxisWebService/services/IConditionInfoService?wsdl");
options.setTo(targetEPR);
// Setting the weather
/*
* QName opSetWeather = new QName("http://service.pojo.sample",
* "setWeather"); Weather w = new Weather();
* w.setTemperature((float)39.3); w.setForecast("Cloudy with
* showers"); w.setRain(true); w.setHowMuchRain((float)4.5);

* Object[] opSetWeatherArgs = new Object[] { w };

* serviceClient.invokeRobust(opSetWeather, opSetWeatherArgs);
*/
// Getting the weather
QName opSayHello = new QName("http://impl.service.conditionInfo.database.dlmu.com",
"sayHello");


Object[] opSayHelloArgs = new Object[] {"heipacker"};
Class[] returnTypes = new Class[] { String.class };
Object[] response;


response = serviceClient.invokeBlocking(opSayHello,
opSayHelloArgs, returnTypes);


String result = (String) response[0];


if (result == null) {
System.out.println("sayHello didn't initialize!");
return;
}
// Displaying the result
System.out.println("Temperature               : " + result);
} catch (AxisFault e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


}
}

服务器端运行结果如下:

heipacker:hello world!

客户端运行结果如下:

Temperature               : heipacker:hello world!

原创粉丝点击