随笔 - 312, 文章 - 14, 评论 - 1393, 引用 - 0
数据加载中……

WebService大讲堂之Axis2(7):将Spring的装配JavaBean发布成WebService

本文为原创,如需转载,请注明作者和出处,谢谢!

上一篇:WebService大讲堂之Axis2(6):跨服务会话(Session)管理

   在现今的Web应用中经常使用Spring框架来装载JavaBean。如果要想将某些在Spring中装配的JavaBean发布成WebService,使用Axis2Spring感知功能是非常容易做到的。
    在本文的例子中,除了<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类,并被始化了namejob属性。在配置完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参数指定要发布成WebServiceJava类,而是要指定在applicationContext.xml文件中的装配JavaBean的名称(SpringBeanName参数)。

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

下一篇:
WebService大讲堂之Axis2(8):异步调用WebService





Android开发完全讲义(第2版)(本书版权已输出到台湾)

http://product.dangdang.com/product.aspx?product_id=22741502



Android高薪之路:Android程序员面试宝典 http://book.360buy.com/10970314.html


新浪微博:http://t.sina.com.cn/androidguy   昵称:李宁_Lining

posted on 2009-02-13 09:14 银河使者 阅读(17173) 评论(15)  编辑  收藏 所属分类: javaspring 原创webservice

评论

# re: WebService大讲堂之Axis2(7):将Spring的装配JavaBean发布成WebService  回复  更多评论   

我按照你在文中的说明一步步进行,可是最后启动tomcat,最后发现没有成功部署此webservice。也看见什么异常出来,这是什么原因呢?
2009-04-29 14:22 | Sophie

# re: WebService大讲堂之Axis2(7):将Spring的装配JavaBean发布成WebService  回复  更多评论   

需要在web.xmll文件中加入下面内容,加了吧?
<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>
2009-04-29 15:13 | 银河使者

# re: WebService大讲堂之Axis2(7):将Spring的装配JavaBean发布成WebService  回复  更多评论   

加了。我看tomcat的启动日志,确实也成功读取WEB-INF/applicationContext.xml这个文件了。
2009-04-30 09:43 | Sophie

# re: WebService大讲堂之Axis2(7):将Spring的装配JavaBean发布成WebService[未登录]  回复  更多评论   

我按照你上面的方法做,在ie里面能反问,没有任何错误,但是新建一个客户端(eclipse中自动生成的)调用这个webservice时,SpringServicePortType.java这个类里面的方法返回的参数类型是XXX.GetPersonResponse(期待类型应是java.lang.String).请问这是为什么呀???
2009-05-14 15:54 | aa

# re: WebService大讲堂之Axis2(7):将Spring的装配JavaBean发布成WebService  回复  更多评论   

axis2把有些简单类型封装到一个类中了,你看看自动生成的stub类,里面有类型所指定的类,你建立个对象,赋一下值就可以了。
2009-05-14 16:10 | 银河使者

# re: WebService大讲堂之Axis2(7):将Spring的装配JavaBean发布成WebService[未登录]  回复  更多评论   

我的SpringServicePortType.java这个类里面的方法的返回类型照理应该全部是java.lang.String,但是现在只有第一个不是java.lang.String而是XXX.GetPersonResponse,其他的几个方法返回的类型都是正确的。不明白为什么?
2009-05-14 16:29 | aa

# re: WebService大讲堂之Axis2(7):将Spring的装配JavaBean发布成WebService  回复  更多评论   

好文章,受益了。
再指出一点注意的地方,
<parameter name="ServiceObjectSupplier"> org.apache.axis2.extensions.spring.receivers.SpringServletContextObjectSupplier
</parameter>
里面的类一定不要写错,在别的文章里看到有用别的类的,可能是不用场合。如果运行有问题,可以考虑下这个地方。
2009-05-30 23:03 | wqyang

# re: WebService大讲堂之Axis2(7):将Spring的装配JavaBean发布成WebService  回复  更多评论   

我照你写的做的,可以启动TOMCAT的时候报这个错误,ERROR] Context initialization failed
org.springframework.beans.factory.BeanDefinitionStoreException: Line 9 in XML document from ServletContext resource [/WEB-INF/applicationContext.xml] is invalid; nested exception is org.xml.sax.SAXParseException: Document root element "beans", must match DOCTYPE root "null".
2009-07-17 13:28 | mumu

# re: WebService大讲堂之Axis2(7):将Spring的装配JavaBean发布成WebService  回复  更多评论   

可能是写错了,把你写的贴上来看看。配置文件。
2009-07-17 14:09 | 银河使者

# re: WebService大讲堂之Axis2(7):将Spring的装配JavaBean发布成WebService  回复  更多评论   

@mumu
Spring.jar 包问题,解析applicationContext.xml文件出错了。
2009-07-27 14:48 | Elimin

# re: WebService大讲堂之Axis2(7):将Spring的装配JavaBean发布成WebService[未登录]  回复  更多评论   

你好,我部署的时候发生这个错误怎么回事啊?
[ERROR] Context initialization failed
org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [service.SpringService] for bean with name 'springService' defined in ServletContext resource [/WEB-INF/applicationContext.xml]; nested exception is java.lang.ClassNotFoundException: service.SpringService
2009-12-14 18:49 | laimu

# re: WebService大讲堂之Axis2(7):将Spring的装配JavaBean发布成WebService  回复  更多评论   

帮我看看一个异常可以吗?在线等

org.apache.axis2.deployment.DeploymentException: The following error occurred during schema generation: null
at org.apache.axis2.deployment.ServiceGroupBuilder.populateServiceGroup(ServiceGroupBuilder.java:106)
at org.apache.axis2.deployment.repository.util.ArchiveReader.buildServiceGroup(ArchiveReader.java:110)
at org.apache.axis2.deployment.repository.util.ArchiveReader.processServiceGroup(ArchiveReader.java:179)
at org.apache.axis2.deployment.ServiceDeployer.deploy(ServiceDeployer.java:81)
at org.apache.axis2.deployment.repository.util.DeploymentFileData.deploy(DeploymentFileData.java:136)
at org.apache.axis2.deployment.DeploymentEngine.doDeploy(DeploymentEngine.java:597)
at org.apache.axis2.deployment.repository.util.WSInfoList.update(WSInfoList.java:144)
at org.apache.axis2.deployment.RepositoryListener.update(RepositoryListener.java:330)
at org.apache.axis2.deployment.RepositoryListener.checkServices(RepositoryListener.java:227)
at org.apache.axis2.deployment.DeploymentEngine.loadServices(DeploymentEngine.java:131)
at org.apache.axis2.deployment.FileSystemConfigurator.loadServices(FileSystemConfigurator.java:147)
at org.apache.axis2.context.ConfigurationContextFactory.createConfigurationContext(ConfigurationContextFactory.java:82)
at org.apache.axis2.context.ConfigurationContextFactory.createConfigurationContextFromFileSystem(ConfigurationContextFactory.java:184)
at com.ncs.saas.comm.message.WebServiceClient.main(WebServiceClient.java:26)
Caused by: org.apache.axis2.deployment.DeploymentException: The following error occurred during schema generation: null
at org.apache.axis2.deployment.ServiceBuilder.populateService(ServiceBuilder.java:431)
at org.apache.axis2.deployment.ServiceGroupBuilder.populateServiceGroup(ServiceGroupBuilder.java:101)
... 13 more
Caused by: org.apache.axis2.deployment.DeploymentException: The following error occurred during schema generation: null
at org.apache.axis2.deployment.ServiceBuilder.populateService(ServiceBuilder.java:394)
... 14 more
Caused by: java.security.PrivilegedActionException: java.lang.reflect.InvocationTargetException
at org.apache.axis2.java.security.AccessController.doPrivileged(AccessController.java:136)
at org.apache.axis2.deployment.util.Utils.fillAxisService(Utils.java:485)
at org.apache.axis2.deployment.ServiceBuilder.populateService(ServiceBuilder.java:386)
... 14 more
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.axis2.deployment.util.Utils$16.run(Utils.java:491)
at org.apache.axis2.java.security.AccessController.doPrivileged(AccessController.java:132)
... 16 more
Caused by: org.apache.axis2.AxisFault: Axis2 Can't find ServletConfigParameter
at org.apache.axis2.AxisFault.makeFault(AxisFault.java:430)
at org.apache.axis2.extensions.spring.receivers.SpringServletContextObjectSupplier.getServiceObject(SpringServletContextObjectSupplier.java:86)
... 22 more
Caused by: java.lang.Exception: Axis2 Can't find ServletConfigParameter
at org.apache.axis2.extensions.spring.receivers.SpringServletContextObjectSupplier.getServiceObject(SpringServletContextObjectSupplier.java:62)
... 22 more
2010-01-15 11:06 | huajian

# re: WebService大讲堂之Axis2(7):将Spring的装配JavaBean发布成WebService  回复  更多评论   

如果我不希望Spring封装的Bean的所有Public方法都公布成WebService,是不是我需要用Operate Tag来配置,而不能使用
<messageReceivers>
<messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out"
class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
</messageReceivers>
2010-09-11 10:57 | A.L

# re: WebService大讲堂之Axis2(7):将Spring的装配JavaBean发布成WebService  回复  更多评论   

我跟着您的步骤做了,虽然配置成功,http://localhost:8080/axis2/services/springService?wsdl也输出了结果,但是我却没在当中找到applicationContext.xml中springService bean初始化的姚明、中国男蓝信息,如果是没有的话那么这个初始化有何作用呢?如何输出这个初始化信息?
2014-05-23 10:22 | skyman531

# re: WebService大讲堂之Axis2(7):将Spring的装配JavaBean发布成WebService  回复  更多评论   

很好,就是有一点,原文中的将上面两个Java源文件编译后,放到<Tomcat安装目录>\webapps\axis2\WEB-INF\classes目录中。应该要加上各自的包,就是要多建两个文件夹,把class文件分别放到对应的目录中去。
2014-07-28 11:22 | yyyyy

只有注册用户登录后才能发表评论。


网站导航: