Axis2在Web项目中整合Spring

时间:2022-07-15 15:25:21

一、说明:

上一篇说了Axis2与Web项目的整合(详情 :Axis2与Web项目整合)过程,如果说在Web项目中使用了Spring框架,那么又改如何进行Axis2相关的配置操作呢?

二、Axis2 与 Spring 整合

①   新建项目 AxisSpringDemo,并在其中加入 Axis2 与 Spring 相关的 jar 包

Spring所需 Jar :

aopalliance-1.0.jar
aspectjrt.jar
aspectjweaver.jar
spring-aop-3.2.1.RELEASE.jar
spring-beans-3.2.1.RELEASE.jar
spring-context-3.2.1.RELEASE.jar
spring-core-3.2.1.RELEASE.jar
spring-expression-3.2.1.RELEASE.jar
spring-tx-3.2.1.RELEASE.jar
spring-web-3.2.1.RELEASE.jar

Axis2 所需 Jar :

activation-1.1.jar
axiom-api-1.2.15.jar
axiom-impl-1.2.15.jar
axis2-adb-1.6.4.jar
axis2-jaxws-1.6.4.jar
axis2-kernel-1.6.4.jar
axis2-spring-1.6.4.jar
axis2-transport-http-1.6.4.jar
axis2-transport-local-1.6.4.jar
axis2-xmlbeans-1.6.4.jar
commons-fileupload-1.3.1.jar
commons-httpclient-3.1.jar
commons-io-2.1.jar
commons-logging-1.1.1.jar
geronimo-stax-api_1.0_spec-1.0.1.jar
httpcore-4.0.jar
jsr311-api-1.1.1.jar
mail-1.4.jar
neethi-3.0.2.jar
woden-api-1.0M9.jar
wsdl4j-1.6.2.jar
xml-resolver-1.2.jar
XmlSchema-1.4.7.jar

②  在工程中的 web.xml 文件中加入 Spring 、 Axis2支持配置:

 <!-- 加入Spring支持 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <!--加入Axis2支持 -->
<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>

③  同上篇讲的整合 web项目一致 ,将conf 、axis2-web  、modules文件夹移动到 AxisSpringDemo工程的 下各个对应的位置。如图:

Axis2在Web项目中整合Spring

④  在src下新建包  com.elgin.spring.webservice ,并新建提供WebService服务的类  SpringWebServiceDemo ,代码如下:

package com.elgin.spring.webservice;

import java.util.Random;

import org.springframework.stereotype.Component;

@Component("springWebService")
public class SpringWebServiceDemo { public String springHello(){
return "hello spring-axis2";
} public int getAge(){
return new Random().nextInt(80);
} public void update(){
System.out.println("update something..");
}
}

⑤  在类路径下新建 Spring配置文件 :applicationContxt.xml 配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"> <!-- 配置spring注解扫描的包 -->
<context:component-scan base-package="com.elgin"></context:component-scan> </beans>

⑥  配置 Axis2的WebService服务:

同上一篇所说:在 AxisWebDemo 工程的 WEB-INF 下新建如下层次结构目录 : services/springServices/META-INF/services.xml

services.xml配置内容:

<?xml version="1.0" encoding="UTF-8"?>
<serviceGroup>
<service name="springService">
<description>Web Service</description>
<!--
作用类似于普通配置中的 ServiceClass ,都是用来创建服务类对象,
只不过普通配置使用反射来创建
加入Spring之后,对象的创建交给了Spring的IOC容器
-->
<parameter name="SpringBeanName">springWebService</parameter>
<parameter name="ServiceObjectSupplier">org.apache.axis2.extensions.spring.receivers.SpringServletContextObjectSupplier</parameter>
<!--
在这里最值得注意的是<messageReceivers>元素,该元素用于设置处理WebService方法的处理器。
例如,getAge方法有一个返回值,因此,需要使用可处理输入输出的RPCMessageReceiver类,
而update方法没有返回值,因此,需要使用只能处理输入的RPCInOnlyMessageReceiver类。
-->
<messageReceivers>
<messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out" class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
<messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-only" class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" />
</messageReceivers>
</service>
</serviceGroup>

三、测试总结:

经过上述的步骤,配置结束,将项目装载的 Tomcat ,启动 ,访问:http://localhost:8080/AxisSpringDemo/services/listServices 出现如下界面:

Axis2在Web项目中整合Spring

通过上面的测试可以发现:

加入Spring之后,除了spring的引入以及配置,唯一不同的地方就是 services.xml 的配置发生了变化