(感觉BlogJava很有人气,就把原来写在msn space上的也转过来)
通常在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 Service1
//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 Service1 /**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
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的详细描述!