---
title: "AIXS2和Spring集成"
author: "douh"
date: "2010-11-25T16:02:24Z"
tags: []
categories: ["编程语言"]
slug: "aixs2-spring"
draft: false
lastmod: "2010-11-25T16:02:24Z"
id: 824610
url: "https://douh.iteye.com/blog/824610"
---

原文地址：
<
http://blog.csdn.net/xinhaoluan/archive/2008/12/25/3605234.aspx
>
由于项目需要搭建一个良好的架构，我选择了用axis2和spring整合。不过再还没有做的时候，就听同事和网上的朋友说axis2和spring整合是多么难的事，使得我有一种极度想挑战它的心理，哈，其实整合起来非常的容易，不过网上介绍axis2的不多。我是用axis2官方网站的方法做整合的。地址：http://ws.apache.org/axis2/1_4_1/spring.html
官方网站上介绍两种方式整合，一种是我们所熟悉的applicationContext.xml放在和web.xml同一级的目录下,第二种是放在aar里面。因为任务上的一些因素，我选用第一种方式，而且我也只记录第一种，第二种我没有研究我，我怕说错了误人子弟，好啦，开始。

1、首先，需要axis2.war，根据它的目录结构我们修改自己的web应用。只需拷贝axis2/WEB-INF/目录下的所有文件到自己的web项目下就可以了，记得要覆盖所有（axis2-web这个文件夹是包含axis2整个的管理界面，可有可无，不过要用的话，记得它是放在和WEB-INF的同级目录下），其实我们就是有点像在整个的axis2工程上做二次开发。

2、我们像做所有其它的Web项目一样，用到spring的时候拷贝spring.jar到WEB-INF/lib目录下，然后在web.xml同级的目录下建立一个 applicationContext.xml，并且在applicationContext.xml引用相应的DTD或XSD（spring1.2引用的是DTD，spring2.0引用的是XSD），然后会在web.xml中加入这一段（当然，你可以选择用其它方式配置spring对web项目的支持）。

1.< listener > 
2.         < listener-class > org.springframework.web.context.ContextLoaderListener </ listener-class > 
3.</ listener > 
4.< context-param > 
5.       < param-name > contextConfigLocation </ param-name > 
6.       < param-value > /WEB-INF/applicationContext.xml </ param-value > 
7.</ context-param > 
3、发布整个项目发部到tomcat上，测试起动服务有没有问题。（一般不会是有问题，我们现在做的只是在axis2这个工程里添加了spring的jar包和在原有的axis2工程的web.xml加上了对spring的支持）。起动没问题的话就访问下http://<host>:<port>/<project name>/services/Version.wsdl，只要成功就可以了，下面进入正题。

4、写一个接口。（体现spring面向接口编程的好处）

1.package com.hp.bean; 
2.
3.public   interface  MyBean { 
4.
5.     public  String echo(); 
6.} 
5、实现这个接口。

1.package com.hp.bean; 
2.
3.public   class  MyBeanImpl implements MyBean { 
4.
5.     public  String echo() { 
6.         
7.         return   "Merry" ; 
8.    } 
9.} 
6、写一个Web Service。

1.package com.hp.service; 
2.
3.import com.hp.bean.MyBean; 
4.
5.public   class  SpringAwareService { 
6.
7.     private  MyBean myBean; 
8.
9.     public   void  setMyBean(MyBean myBean) { 
10.         this .myBean = myBean; 
11.    } 
12.     
13.     public  String response(){ 
14.         
15.         return  myBean.echo() +  " Christmas!" ; 
16.    } 
17.} 
7、编写applicationContext.xml.（记住第一个bean,也就是applicationContext那个一定要有）

1.    <bean id= "applicationContext" 
2.         class = "org.apache.axis2.extensions.spring.receivers.ApplicationContextHolder"  /> 
3.
4.    <bean id= "springAwareService" 
5.         class = "com.hp.service.SpringAwareService" > 
6.        <property name= "myBean"   ref = "myBean"  /> 
7.    </bean> 
8.
9.    <bean id= "myBean"   class = "com.hp.bean.MyBeanImpl" ></bean> 
8、在WEB-INF/services/下新建一个文件夹，这个名字可以起的随便，我们就起做test吧，然后在WEB-INF/services/test/下建立META-INF这个目录，最后在WEB-INF/services/test/META-INF/下建立service.xml,文件内容是：

1.<service name= "SpringAwareService" > 
2.    <description>simple spring example</description> 
3.    <parameter name= "ServiceObjectSupplier" > 
4.        org.apache.axis2.extensions.spring.receivers.SpringAppContextAwareObjectSupplier 
5.    </parameter> 
6.    <parameter name= "SpringBeanName" >springAwareService</parameter> 
7.    <messageReceivers> 
8.        <messageReceiver mep= "http://www.w3.org/2004/08/wsdl/in-only" 
9.             class = "org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver"  /> 
10.        <messageReceiver mep= "http://www.w3.org/2004/08/wsdl/in-out" 
11.             class = "org.apache.axis2.rpc.receivers.RPCMessageReceiver"  /> 
12.    </messageReceivers> 
13.</service> 
这回再发布一下，在没有异常的情况下。访问一下http://<host>:<port>/<project name>/services/SpringAwareService?wsdl 看看



本文来自CSDN博客，转载请标明出处：http://blog.csdn.net/xinhaoluan/archive/2008/12/25/3605234.aspx