axis2整合spring .

由于项目需要搭建一个良好的架构,我选择了用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项目的支持)。

<listener>
    <listener-class>
	org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener>
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>

3、发布整个项目发部到tomcat上,测试起动服务有没有问题。(一般不会是有问题,我们现在做的只是在axis2这个工程里添加了spring的jar包和在原有的axis2工程的web.xml加上了对spring的支持)。起动没问题的话就访问下http://://services/Version?wsdl,只要成功就可以了,下面进入正题。

4、写一个接口。(体现spring面向接口编程的好处)

package com.hp.bean;

public interface MyBean{

     public String echo();
}

5、实现这个接口。

package com.hp.bean;

public class MyBeanImpl implements MyBean {

     public String echo(){
         return "Merry";
    }
} 

6、写一个Web Service。

package com.hp.service;

import com.hp.bean.MyBean;

public class SpringAwareService{

    private MyBean myBean;

    public void setMyBean(MyBean myBean){
        this .myBean = myBean;
    }
     
    public String response(){
        return myBean.echo() + " Christmas!";
    }
 }

7、编写applicationContext.xml.(记住第一个bean,也就是applicationContext那个一定要有)

<bean id="applicationContext" 
class= "org.apache.axis2.extensions.spring.receivers.ApplicationContextHolder"/> 

<bean id="springAwareService"class="com.hp.service.SpringAwareService"> 
    <property name="myBean" ref="myBean"/>
</bean>

<bean id="myBean" class="com.hp.bean.MyBeanImpl"/> 

8、在WEB-INF/services/下新建一个文件夹,这个名字可以起的随便,我们就起做test吧,然后在WEB-INF/services/test/下建立META-INF这个目录,最后在WEB-INF/services/test/META-INF/下建立service.xml,文件内容是:

<service name= "SpringAwareService"> 
    <description>simple spring example</description> 
    <parameter name="ServiceObjectSupplier">
    org.apache.axis2.extensions.spring.receivers.SpringAppContextAwareObjectSupplier 
    </parameter> 
    <parameter name="SpringBeanName">springAwareService</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> 

 
这回再发布一下,在没有异常的情况下。访问一下http://://services/SpringAwareService?wsdl 看看

转自:http://blog.csdn.net/xinhaoluan/article/details/3605234

猜你喜欢

转载自sanhye.iteye.com/blog/2076179