Axis2在Web项目中整合Spring

来源:互联网 发布:飞鸽传书需要网络吗 编辑:程序博客网 时间:2024/06/02 13:13

一、说明:

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

二、Axis2 与 Spring 整合

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

Spring所需 Jar :

aopalliance-1.0.jaraspectjrt.jaraspectjweaver.jarspring-aop-3.2.1.RELEASE.jarspring-beans-3.2.1.RELEASE.jarspring-context-3.2.1.RELEASE.jarspring-core-3.2.1.RELEASE.jarspring-expression-3.2.1.RELEASE.jarspring-tx-3.2.1.RELEASE.jarspring-web-3.2.1.RELEASE.jar
Axis2 所需 Jar :

activation-1.1.jaraxiom-api-1.2.15.jaraxiom-impl-1.2.15.jaraxis2-adb-1.6.4.jaraxis2-jaxws-1.6.4.jaraxis2-kernel-1.6.4.jaraxis2-spring-1.6.4.jaraxis2-transport-http-1.6.4.jaraxis2-transport-local-1.6.4.jaraxis2-xmlbeans-1.6.4.jarcommons-fileupload-1.3.1.jarcommons-httpclient-3.1.jarcommons-io-2.1.jarcommons-logging-1.1.1.jargeronimo-stax-api_1.0_spec-1.0.1.jarhttpcore-4.0.jarjsr311-api-1.1.1.jarmail-1.4.jarneethi-3.0.2.jarwoden-api-1.0M9.jarwsdl4j-1.6.2.jarxml-resolver-1.2.jarXmlSchema-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工程的 下各个对应的位置。如图:


④  在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.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsdhttp://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 出现如下界面:

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

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

4 0
原创粉丝点击