在现今的Web应用中经常使用Spring框架来装载JavaBean。如果要想将某些在Spring中装配的JavaBean发布成WebService,使用Axis2的Spring感知功能是非常容易做到的。
    在本文的例子中,除了<Tomcat安装目录>\webapps\axis2目录及该目录中的相关库外,还需要Spring框架中的spring.jar文件,将该文件复制到<Tomcat安装目录>\webapps\axis2\WEB-INF\lib目录中。
    下面先建立一个JavaBean(该JavaBean最终要被发布成WebService),代码如下:

package service;


import entity.Person;


public 
class SpringService

{

    
private String name;

    
private String job;

    
public 
void setName(String name)

    {

        
this.name = name;

    }

    
public 
void setJob(String job)

    {

        
this.job = job;

    }

    
public Person getPerson()

    {

        Person person = 
new Person();

        person.setName(name);

        person.setJob(job);

        
return person;

    }

    
public String getGreeting(String name)

    {

        
return "hello " + name;

    }

}

其中Person也是一个JavaBean,代码如下:

package entity;


public 
class Person

{

    
private String name;

    
private String job;

    
public String getName()

    {

        
return name;

    }

    
public 
void setName(String name)

    {

        
this.name = name;

    }

    
public String getJob()

    {

        
return job;

    }

    
public 
void setJob(String job)

    {

        
this.job = job;

    }

}

将上面两个Java源文件编译后,放到<Tomcat安装目录>\webapps\axis2\WEB-INF\classes目录中。

在<Tomcat安装目录>\webapps\axis2\WEB-INF\web.xml文件中加入下面的内容:

<
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
>

在<Tomcat安装目录>\webapps\axis2\WEB-INF目录中建立一个applicationContext.xml文件,该文件是Spring框架用于装配JavaBean的配置文件,内容如下:

<?
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:tx
="http://www.springframework.org/schema/tx"

        xsi:schemaLocation
="
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
>

  
<
bean 
id
="springService"
 class
="service.SpringService"
>

     
<
property 
name
="name"
 value
="姚明"
 
/>

     
<
property 
name
="job"
 value
="职业男篮"
 
/> 

  
</
bean
>   


</
beans
>

在applicationContext.xml文件中装配了service.SpringService类,并被始化了name和job属性。在配置完SpringService类后,就可以直接在程序中FileSystemXmlApplicationContext类或其他类似功能的类读取applicationContext.xml文件中的内容,并获得SpringService类的对象实例。但现在我们并不这样做,而是将SpringService类发布成WebService。

在<Tomcat安装目录>\webapps\axis2\WEB-INF\lib目录中有一个axis2-spring-1.4.1.jar文件,该文件用于将被装配JavaBean的发布成WebService。在D盘建立一个axi2-spring-ws目录,并在该目录中建立一个META-INF子目录。在META-INF目录中建立一个services.xml文件,内容如下:

<
service 
name
="springService"
>

    
<
description
>

        Spring aware

    
</
description
>

    
<
parameter 
name
="ServiceObjectSupplier"
>

        org.apache.axis2.extensions.spring.receivers.SpringServletContextObjectSupplier

    
</
parameter
>

    
<
parameter 
name
="SpringBeanName"
>

        springService

    
</
parameter
>

    
<
messageReceivers
>

        
<
messageReceiver 
mep
="http://www.w3.org/2004/08/wsdl/in-out"

            class
="org.apache.axis2.rpc.receivers.RPCMessageReceiver"
 
/>

    
</
messageReceivers
>


</
service
>     

在Windows控制台进入axi2-spring-ws目录,并使用jar命令将axi2-spring-ws目录中的内容打包成axi2-spring-ws.aar,然后将该文件复制到<Tomcat安装目录>\webapps\axis2\WEB-INF\services目录中,启动Tomcat后,就可以访问该WebService了,访问方式与前面几篇文章的访问方式相同。获得wsdl内容的URL如下:

http://localhost:8080/axis2/services/springService?wsdl
    在将Spring中的装配JavaBean发布成WebService需要注意以下几点:

1. 由JavaBean编译生成的.class文件需要放在WEB-INF\classes目录中,或打成.jar包后放在WEB-INF\lib目录中,而WEB-INF\services目录中的.aar包中不需要包含.class文件,而只需要包含一个META-INF目录,并在该目录中包含一个services.xml文件即可。

2. services.xml的配置方法与前几篇文章的配置方法类似,只是并不需要使用ServiceClass参数指定要发布成WebService的Java类,而是要指定在applicationContext.xml文件中的装配JavaBean的名称(SpringBeanName参数)。

3. 在services.xml文件中需要通过ServiceObjectSupplier参数指定SpringServletContextObjectSupplier类来获得Spring的ApplicationContext对象。

Axis2(7):将Spring的装配JavaBean发布成WebService的更多相关文章

  1. SAP ECC EHP7 RFC 发布成WebService

    1.说明介绍 本文将RFC发布成WebService的详细步骤,参考了百度经验http://jingyan.baidu.com/article/8275fc867c9e2946a13cf66c.htm ...

  2. 带你走进EJB--将EJB发布为Webservice(4)

    接下来的我们将会自定义一个对象,然后看看EJB是如何对复杂的参数发布成WebService的. 代码如下:在第一个版本的基础之上加上增加用户的方法,参数为User. package com.tgb.e ...

  3. 带你走进EJB--将EJB发布为Webservice(1)

    Web service是一个平台独立,松耦合基于可编程的web的应用程序,可使用开放的XML标准来描述.发布.发现.协调和配置这些应用程序,用于开发分布式的互操作的应用程序. 简单说Web servi ...

  4. 解决cxf+spring发布的webservice,types,portType和message以import方式导入

    用cxf+spring发布了webservice,发现生成的wsdl的types,message和portType都以import的方式导入的.. 原因:命名空间问题 我想要生成的wsdl在同个文件中 ...

  5. Spring整合CXF之发布WebService服务

    今天我们来讲下如何用Spring来整合CXF,来发布WebService服务: 给下官方文档地址:http://cxf.apache.org/docs/writing-a-service-with-s ...

  6. Spring 自动装配 Bean

    Spring3系列8- Spring 自动装配 Bean 1.      Auto-Wiring ‘no’ 2.      Auto-Wiring ‘byName’ 3.      Auto-Wiri ...

  7. Spring整合CXF,发布RSETful 风格WebService(转)

    Spring整合CXF,发布RSETful 风格WebService 这篇文章是承接之前CXF整合Spring的这个项目示例的延伸,所以有很大一部分都是一样的.关于发布CXF WebServer和Sp ...

  8. Spring整合CXF,发布RSETful 风格WebService

    原文地址:http://www.cnblogs.com/hoojo/archive/2012/07/23/2605219.html 这篇文章是承接之前CXF整合Spring的这个项目示例的延伸,所以有 ...

  9. xfire发布的Webservice中Spring注入为空的解决方案

    Spring框架使用中注入为空是一个比较头疼的问题,遇到Webservice和Spring框架配合时,这个问题更容易出现并很难发现问题的原因. 在做SSO系统中就遇到这样的问题,在Service的实现 ...

随机推荐

  1. c++ enum用法【转】

    1.为什么要用enum       写程序时,我们常常需要为某个对象关联一组可选alternative属性.例如,学生的成绩分A,B,C,D等,天气分sunny, cloudy, rainy等等.   ...

  2. windows上放弃使用PyGTK

    windows上放弃使用PyGTK - riag的专栏 - 博客频道 - CSDN.NET windows上放弃使用PyGTK 分类: python 2010-03-31 16:47 1054人阅读 ...

  3. 概率图模型(PGM)学习笔记(三)模式判断与概率图流

    我们依旧使用"学生网络"作为样例,如图1. 图1 首先给出因果判断(Causal Reasoning)的直觉解释. 能够算出来 即学生获得好的推荐信的概率大约是0.5. 但假设我们 ...

  4. 框架技术--Spring自动加载配置

    今天项目中遇到一个问题,一个方法在服务启动后会自动被执行,查看了下配置未发现有定时的配置.但是后来发现是spring配置了启动时默认加载了方法. 代码: <?xml version=" ...

  5. 【转】利用Ajax.BeginForm提交文件

    Ajax.BeginForm @using (Ajax.BeginForm("YourAction", "YourController", new AjaxOp ...

  6. Razor强类型视图下的文件上传

    域模型Users.cs using System;using System.Collections.Generic;using System.Linq;using System.Web; namesp ...

  7. 分页:T-SQL存储过程和EF存储过程的使用

    首先准备好分页的T-SQL语句: create proc usp_activityFenYe @pageIndex int, @pageSize int, @pageCount int output ...

  8. JAVA虚拟机内存模型

    一.对于Java程序员来说,在虚拟机的自动内存管理机制下,我们不需要为每一个new操作去写匹配的delete/free操作 但是当我们对于内存的管理了解有能够帮助我们理解Java虚拟机的垃圾回收机制. ...

  9. Spring Boot gradle

    最近有写一个电子订单商务网站,使用JAVA8,SPRING,ANGULARJS对项目使用的技术和大家分享. 第一次写博客,哪有不对需要改正的请联系改正. 因为是项目是我给别人做的无法提供源码见谅,我尽 ...

  10. BZOJ 1610: [Usaco2008 Feb]Line连线游戏

    1610: [Usaco2008 Feb]Line连线游戏 Description Farmer John最近发明了一个游戏,来考验自命不凡的贝茜.游戏开始的时 候,FJ会给贝茜一块画着N (2 &l ...