通常在Spring发布Hession,RMI等,是非常方便的,

但是要发布SOAP类型的WebService则要依赖一个独立的Servlet容器(如Tomcat+Axis),

这种Webservice一般还有别的配置文件,比如web.xml,wsdd文件等等

。有时侯,你可能想一台机器上只部署一个Http Soap Service

,这种场合你可能不希望安装一个类似Tomcat的容器,

你更希望发布的时候就是一个服务程序,该程序启动则提供WebService.这篇文章描述一种解决方案。

开发环境:

Spring 1.2.6

XFire 1.0

Jetty 4.2.1

方案描述:我们可以通过XFire的编程接口来创建WebService并嵌入一个HttpServer,

从而来做到在一个独立应用程序中发布Http Service

1

//Create an XFire Service2ObjectServiceFactory serviceFactory=newObjectServiceFactory();3        Service service=serviceFactory.create(Echo.class);4        service.setInvoker(newBeanInvoker(newEchoImpl()));5//Register the service in the ServiceRegistry6XFire xfire=XFireFactory.newInstance().getXFire();7        xfire.getServiceRegistry().register(service);8//Start the HTTP server9XFireHttpServer server=newXFireHttpServer();10        server.setPort(8191);11        server.start();

这样的话,如果发布多个WebSerice则要依次显式的创建 XFire Service,然后再一一注册,

这样显然是不够优雅的。

我们想要让开发者在Spring配置文件中指定要发布为WebService的POJOs,

然后载入Spring环境就自动发布为webservice,而不需要跟 XFire API打交道。

首先,我们想要一个BeanFacory,能把一个pojo装配成XFire Service

1 /**2 *3 */4 packagecom.yovn.ws.xfire.example;5 6 importorg.codehaus.xfire.service.Service;7 importorg.codehaus.xfire.service.binding.BeanInvoker;8 importorg.codehaus.xfire.service.binding.ObjectServiceFactory;9 importorg.springframework.beans.factory.FactoryBean;10 11 /**12 *@authornew13 *14 */15 publicclassXFireServiceFactoryBeanimplementsFactoryBean16 {17 18 19 20 21 privateClass serviceClass;22 23 24 privateObject target;25 26 27 privateService service;28 29 30 privatefinalObjectServiceFactory sf=newObjectServiceFactory();31 32 /**33 *34 */35 publicXFireServiceFactoryBean()36 {37 38 }39 40 /*(non-Javadoc)41 * @see org.springframework.beans.factory.FactoryBean#getObject()42 */43 publicObject getObject()throwsException44 {45 if(service==null)46 {47 service=sf.create(serviceClass);48 service.setInvoker(newBeanInvoker(target));49 }50 returnservice;51 }52 53 /*(non-Javadoc)54 * @see org.springframework.beans.factory.FactoryBean#getObjectType()55 */56 publicClass getObjectType()57 {58 59 returnService.class;60 }61 62 /*(non-Javadoc)63 * @see org.springframework.beans.factory.FactoryBean#isSingleton()64 */65 publicbooleanisSingleton()66 {67 returntrue;68 }69 70 publicvoidsetServiceClass(Class serviceClass)71 {72 this.serviceClass=serviceClass;73 }74 75 publicvoidsetTarget(Object target)76 {77 this.target=target;78 }79 80 }81

这样我们可以通过Spring来装配一个pojo,

下一步我们要在Spring容器载入时候注册XFire Service,

并启动一个嵌入的Http Server,我们可以借助Spring的ApplicationListener来实现

1 /**2 *3 */4 packagecom.yovn.ws.xfire.example;5 6 7 importorg.apache.commons.logging.Log;8 importorg.apache.commons.logging.LogFactory;9 importorg.codehaus.xfire.XFire;10 importorg.codehaus.xfire.XFireFactory;11 importorg.codehaus.xfire.server.http.XFireHttpServer;12 importorg.codehaus.xfire.service.Service;13 importorg.codehaus.xfire.service.ServiceRegistry;14 importorg.springframework.context.ApplicationContext;15 importorg.springframework.context.ApplicationEvent;16 importorg.springframework.context.ApplicationListener;17 importorg.springframework.context.event.ContextClosedEvent;18 importorg.springframework.context.event.ContextRefreshedEvent;19 20 /**21 *@authornew22 *23 */24 publicclassXFireServiceStarterimplementsApplicationListener25 {26 27 privateintport=80;28 29 privateXFireHttpServer server;30 privatefinalLog logger=LogFactory.getLog(getClass().getName());31 32 publicvoidsetPort(intport)33 {34 this.port=port;35 }36 37 publicvoidonApplicationEvent(ApplicationEvent event)38 {39 try40 {41 if(eventinstanceofContextRefreshedEvent)42 {43 44 if(server!=null)45 {46 47 server.stop();48 logger.info("xfire server stopped

");49 50 }51 registerService((ApplicationContext)event.getSource());52 server=newXFireHttpServer();53 server.setPort(port);54 server.start();55 logger.info("xfire server started

");56 57 }elseif(eventinstanceofContextClosedEvent)58 {59 if(server!=null)60 {61 server.stop();62 logger.info("xfire server stopped

");63 }64 65 }66 67 }catch(Exception e)68 {69 logger.error("process event"+event+"error",e);70 }71 72 }73 74 privatevoidregisterService(ApplicationContext context)75 {76 XFire xfire=XFireFactory.newInstance().getXFire();77 ServiceRegistry registry=xfire.getServiceRegistry();78 String names[]=context.getBeanNamesForType(Service.class);79 80 for(inti=0;i

Ok,我们完成基础的代码,下面试着发布一个简单的WebServie

1 packagecom.yovn.ws.xfire.example;2 3 publicinterfaceAdd4 {5 intadd(inta,intb);6 7 }

该接口的实现如下

1 packagecom.yovn.ws.xfire.example;2 3 publicclassAddImplimplementsAdd4 {5 6 publicintadd(inta,intb)7 {8 9 returna+b;10 }11 12 }

这是一个简单功能的POJO,下面我们在Spring中装配起来

1 <?xml  version="1.0" encoding="UTF-8"?>2 beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">3 4 5 6 7 8 9 10 11 12 13

好了,我们完成了,只要载入这个xml初始化一个Spring ApplicationContext,一个名叫Add的webservice就发布了。你可以通过访问http://localhost/Add?wsdl来获得webservice的详细描述!

java http soap_在Spring中发布SOAP HTTP Webservice - Java杂家 - Blo...相关推荐

  1. 原生java读取properties与spring中@value、@ConfigurationProperties读取配置文件

    原生java读取properties与spring中@value.@ConfigurationProperties读取配置文件 1.properties类 Properties 继承于 Hashtab ...

  2. Java EE 7公共草案已发布。 我需要Java EE Light Profile!

    2012年12月20日,Java EE 7的公共草案已上载. 乍一看,新规范是对Java EE 6中后续规范的改进.例如,我真的很喜欢Web Profile的想法. 遗憾的是它不是Java EE 6 ...

  3. jax-ws 生成soap_在Spring中记录JAX-WS SOAP消息

    jax-ws 生成soap 每当在Spring中使用JAX-WS时,您可能都希望记录传入和传出的SOAP消息-如果仅用于开发过程中的调试. 因此,第一件事就是增加日志级别,对吗? 不幸的是,这将无效. ...

  4. java怎么加定时器,Spring中定时器实现

    在一些工作需要使用到定时器,Spring很好的集成了定时器的功能! 在Spring 中使用Quartz,本文介绍Spring3.0以后自主开发的定时任务工具,spring task,可以将它比作一个轻 ...

  5. spring 加载java类_在Spring中基于Java类进行配置的完整步骤

    在Spring中基于Java类进行配置的完整步骤 发布于 2020-7-7| 复制链接 基于Java配置选项,可以编写大多数的Spring不用配置XML,下面 前言JavaConfig 原来是 Spr ...

  6. Java笔记-AnnotationConfigApplicationContext在Spring中的例子

    Bean在Spring中是可以代替xml文件的.Bean在@Configuration中被创建. 应用程序的配置通过AnnotationConfigApplication加载的.AnnotationC ...

  7. java spring server_java server之spring中的IOC如何用java实现?

    ** 什么是IOC? 一般的对象耦合是在编译时确定的,也就是说当我们写如下类: public class StaticCoupling { String s = new String("hz ...

  8. java 删除二维数组中的null_避免在Java中检查Null语句

    1.概述 通常,在Java代码中处理null变量.引用和集合很棘手.它们不仅难以识别,而且处理起来也很复杂.事实上,在编译时无法识别处理null的任何错误,会导致运行时NullPointerExcep ...

  9. java调度:(四) spring中使用quartz的配置文件.

    quartz主要是三个部分:Scheduler Job Trigger,其中,Job 负责定义需要执行的任务,Trigger 负责设置调度策略,Scheduler 将二者组装在一起,并触发任务开始执行 ...

最新文章

  1. BN和Dropout在训练和测试时有哪些差别?
  2. 聊聊微服务的服务注册与发现
  3. xshell突然连接不上虚拟机解决办法
  4. 2019 年 React 学习路线图
  5. 关闭 启动_Steam如何关闭开机自动启动
  6. ABAP git客户端的简单介绍
  7. 系统I/O小程序-文件拷贝
  8. [vue] 你期待vue3.0有什么功能或者改进的地方?
  9. Android自己定义DataTimePicker(日期选择器)
  10. pytorch多gpu并行训练
  11. C++ string split()和 replaceAll()
  12. 巧用MacOS的勿扰模式,解决广告弹窗
  13. 安装python3-dev_ubuntu16.04安装python3.7
  14. Improving Opencv 8: The Core Functionality :File Input and Output using XML and YAML files
  15. 联想T110 8G优盘 安国AU6983主控量产成功
  16. 【随机过程】10 -高斯过程与布朗运动
  17. 高德地图开放平台的使用
  18. pkg-php,node打包工具Pkg(详细教程)
  19. Androidstudio setting .DEX extension only for .CLASS files
  20. Bootstrap4——字体大小根据屏幕改变解决方案

热门文章

  1. 软件工程学习进度第十周汇总
  2. DataTable分组
  3. 又到开学季 学习神器走一波 物联网开发板
  4. git push失败:ssh:connect to host github.com port 22:Connection timed out
  5. ubuntu16.04中IDEA无法输入中文问题
  6. 【CodeVS】1993草地排水
  7. 一句话总结重构、重载、重写
  8. NHibernate+WCF项目实战(四)使用WAS对Webservices接口进行压力测试
  9. 删除iptables nat 规则
  10. 韩顺平 servlet 笔记 第4讲