一、说明:

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

二、Axis2 与 Spring 整合

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

Spring所需 Jar :




1. aopalliance-1.0.jar  
2. aspectjrt.jar
3. aspectjweaver.jar
4. spring-aop-3.2.1.RELEASE.jar
5. spring-beans-3.2.1.RELEASE.jar
6. spring-context-3.2.1.RELEASE.jar
7. spring-core-3.2.1.RELEASE.jar
8. spring-expression-3.2.1.RELEASE.jar
9. spring-tx-3.2.1.RELEASE.jar
10. spring-web-3.2.1.RELEASE.jar


Axis2 所需 Jar :



1. activation-1.1.jar  
2. axiom-api-1.2.15.jar
3. axiom-impl-1.2.15.jar
4. axis2-adb-1.6.4.jar
5. axis2-jaxws-1.6.4.jar
6. axis2-kernel-1.6.4.jar
7. axis2-spring-1.6.4.jar
8. axis2-transport-http-1.6.4.jar
9. axis2-transport-local-1.6.4.jar
10. axis2-xmlbeans-1.6.4.jar
11. commons-fileupload-1.3.1.jar
12. commons-httpclient-3.1.jar
13. commons-io-2.1.jar
14. commons-logging-1.1.1.jar
15. geronimo-stax-api_1.0_spec-1.0.1.jar
16. httpcore-4.0.jar
17. jsr311-api-1.1.1.jar
18. mail-1.4.jar
19. neethi-3.0.2.jar
20. woden-api-1.0M9.jar
21. wsdl4j-1.6.2.jar
22. xml-resolver-1.2.jar
23. XmlSchema-1.4.7.jar


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




1. <!-- 加入Spring支持 -->  
2. <context-param>
3. <param-name>contextConfigLocation</param-name>
4. <param-value>classpath:applicationContext.xml</param-value>
5. </context-param>
6. <listener>
7. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
8. </listener>
9.
10. <!--加入Axis2支持 -->
11. <servlet>
12. <servlet-name>AxisServlet</servlet-name>
13. <servlet-class>org.apache.axis2.transport.http.AxisServlet</servlet-class>
14. <load-on-startup>1</load-on-startup>
15. </servlet>
16. <servlet-mapping>
17. <servlet-name>AxisServlet</servlet-name>
18. <url-pattern>/services/*</url-pattern>
19. </servlet-mapping>


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


Axis2在Web项目中整合Spring_xml

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


1. package com.elgin.spring.webservice;  
2.
3. import java.util.Random;
4.
5. import org.springframework.stereotype.Component;
6.
7. @Component("springWebService")
8. public class SpringWebServiceDemo {
9.
10. public String springHello(){
11. return "hello spring-axis2";
12. }
13.
14. public int getAge(){
15. return new Random().nextInt(80);
16. }
17.
18. public void update(){
19. "update something..");
20. }
21. }


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


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


⑥  配置 Axis2的WebService服务:

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

services.xml配置内容:



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

三、测试总结:


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


Axis2在Web项目中整合Spring_xml_02

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

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