使用xfire创建webservice的一般过程
服务程序 1、? 创建一个普通的web工程 使用eclipse的IDE创建一个普通的web project. 2、? 加入必须的jar文件 使用xfire创建的webservice须用到的jar有: Spring.jar,jdom-1.0.jar,spring-webmvc.jar,commons-logging-1.0.4.jar,xfire-core-1.2.6.jar,vsdl4j-1.6.1.jar,xfire-spring-1.2.6.jar,xfire-aegis-1.2.6.jar 3、? 配置spring 此处使用的1.2xfire,其使用的spring,版本为1.2.6.本例中使用spring2.0. 在web.xml中增加spring监听器 ? ??? <context-param> ?????? <param-name>contextConfigLocation</param-name> ?????? <param-value>classpath:/applicationContext*.xml</param-value> </context-param> ? ??? <listener> ?????? <listener-class> ?????????? org.springframework.web.context.ContextLoaderListener ?????? </listener-class> </listener> 在源码文件夹src中创建spring的配置文档applicationContext.xml, ? 4、? 配置XfireServlet 在web.xml中增加XfireServlet ??? <servlet> ?????? <servlet-name>XFireServlet</servlet-name> ?????? <servlet-class> ?????????? org.codehaus.xfire.spring.XFireSpringServlet ?????? </servlet-class> ??? </servlet> ? ??? <servlet-mapping> ?????? <servlet-name>XFireServlet</servlet-name> ?????? <url-pattern>/services/*</url-pattern> </servlet-mapping> 5、? 导入xfire.xml,配置基类bean 在applicationContext.xml中导入xfire自带的常用的bean,当然也可以不导入,自己在applicationContext.xml中配置。导入方式为 <import resource="classpath:org/codehaus/xfire/spring/xfire.xml" /> 然后配置一个基bean供业务bean继承,只有继承了xfire的bean才能被xfire管理, <bean id="baseWebService" ????????????? class="org.codehaus.xfire.spring.remoting.XFireExporter" ????????????? lazy-init="false" abstract="true"> ????????????? <property name="serviceFactory"> ???????????????????? <ref bean="xfire.serviceFactory" /> ????????????? </property> ????????????? <property name="xfire"> ???????????????????? <ref bean="xfire" /> ????????????? </property> ????????????? <property name="style"> ???????????????????? <value>wrapped</value> ????????????? </property> ????????????? <property name="use"> ???????????????????? <value>literal</value> ????????????? </property> ????????????? <property name="scope"> ???????????????????? <value>application</value> ????????????? </property> ?????? </bean> ? 6、? 创建业务类 接口:EchoServer package com.server; ? public interface EchoServer { ? ??? public String echo(String msg); } 实现类: package com.server.imp; ? import com.server. EchoServer; ? public class EchoServerImp implements EchoServer { ??? public String echo(String msg) { ?????? return "server:"+msg; ??? } } ? 7、? 配置业务webservice 在applicationContext.xml中配置业务bean 注意继承baseWebService <bean id="echoServer" parent="baseWebService"> ?????? <property name="serviceBean"> ?????????? <bean class="com.server.imp.EchoServerImp" /> ?????? </property> ?????? <property name="serviceClass"> ?????????? <value>com.server.EchoServer</value> ?????? </property> ??? </bean> ? 客户程序 1、? 创建一个普通的工程 2、? 加入必须的jar文件 xfire-aegis-1.2.6.jar,wsdl4j-1.6.1.jar,commons-httpclient-3.0.jar,commons-codec-1.3.jar,common-logging-1.0.4.jar 3、? 导入webservice接口 可以创建接口,包名与接口的名字要与服务工程上的一致,也可直接从服务工程上导出包含接口的jar,放入到该项目中,这里是创建的接口. package com.server; ? public interface EchoServer { ? ??? public String echo(String msg); } ? 4、? 编写客户端程序 ? package com.client; ? import java.net.MalformedURLException; ? import org.codehaus.xfire.XFireFactory; import org.codehaus.xfire.client.XFireProxyFactory; import org.codehaus.xfire.service.Service; import org.codehaus.xfire.service.binding.ObjectServiceFactory; ? import com.server.EchoServer; ? public class Test { ? ??? public static void main(String[] args) { ?????? String serviceAddress = "http://127.0.0.1/echoservice/services/EchoServer"; ?????? ObjectServiceFactory objectServiceFactory = new ObjectServiceFactory(); ?????? Service srvcModel = objectServiceFactory.create(EchoServer.class); ?????? XFireProxyFactory factory = new XFireProxyFactory(XFireFactory ????????????? .newInstance().getXFire()); ?????? try { ?????????? EchoServer echoServer = (EchoServer) factory.create( ????????????????? srvcModel,serviceAddress); ?????????? String msg = echoServer.echo("please echo me!"); ?????????? System.out.println(msg); ?????? } catch (MalformedURLException e) { ?????????? e.printStackTrace(); ?????? } ? ??? } } ?转载请标明出处:http://blog.csdn.net/hpujsj/archive/2009/02/07/3867314.aspx (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |