技术开发 频道

使用XFire+Spring构建Web Service



三.开发步骤

1. 工程与环境的建立
MyEclipse中新建Web工程,名为webservice_helloworld。选择该工程后,点击右键选择MyEclipse->Add Web Service Capabilities,弹出Add Web Service Capabilities对话框,点击“Next”,弹出Project Library Configuration对话框,默认选择Core Libraries,点击“Finish”按钮,完成XFire核心包的添加。为了后续的客户端的测试,还需读者加入commons-httpclient.jar包到WEB-INF/lib下。
部署后可看到此时WEB-INF/lib的jar包列表如下:
activation-1.1.jar、commons-beanutils-1.7.0.jar、commons-codec-1.3.jar、commons-httpclient.jar、commons-logging-1.0.4.jar、jaxen-1.1-beta-9.jar、jaxws-api-2.0.jar、jdom-1.0.jar、jsr173_api-1.0.jar、mail-1.4.jar、saaj-api-1.3.jar、saaj-impl-1.3.jar、spring-1.2.6.jar、stax-api-1.0.1.jar、wsdl4j-1.5.2.jar、wstx-asl-3.0.1.jar、xbean-2.1.0.jar、xbean-spring-2.5.jar、xfire-aegis-1.2.2.jar、xfire-annotations-1.2.2.jar、xfire-core-1.2.2.jar、xfire-java5-1.2.2.jar、xfire-jaxws-1.2.2.jar、xfire-jsr181-api-1.0-M1.jar、xfire-spring-1.2.2.jar、XmlSchema-1.1.jar
为了后续的开发和测试,在src目录下分别建立test和webservice目录,分别用于存放测试文件和webservice的相关类。
 
2Web Service实现的编写
    在本例中,我们只是做一个helloWorld的简单例子。Web Service服务端提供一个根据输入的名字信息回复相应的helloWorld信息的。例如,当名字为“阿蜜果”时,恢复信息为“hello,阿蜜果”。下面让我们一步一步来开始进行编码。
1)web.xml的配置
一般情况下,我们通过HTTP作为Web Service的传输协议,这样我们只需启动一个Web服务器(如Tomcat,在本例中使用的是Tomcat5.5.20),这样客户端就可以通过HTTP访问到Web Service服务。为了集成Spring容器,XFire专门提供一个XFireSpringServlet,我们可以在web.xml中配置该Servlet,将Spring容器中定义的Web Service在某个URI下发布。
为了能正确使用XFire,需在web.xml中进行相应配置,在该文件中配置XFire的servletservlet-mapping。同时因为本实例需要将XFire集成到Spring中,因而需要在web.xml文件中加载Spring的相应配置文件。在本实例中,我们首先在WEB-INF下建立两个配置Spring配置文件,一个为applicationContext.xml,该文件用来定义本工程的bean,一个为xfire-servlet.xml,用来配置XFire的相关bean。修改后的web.xml的内容如下所示:

<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>XFireService</display-name> <!-- begin Spring配置 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml,/WEB-INF/xfire-servlet.xml</param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <listener> <listener-class> org.springframework.web.util.IntrospectorCleanupListener </listener-class> </listener> <!-- end Spring配置 --> <!-- begin XFire 配置 --> <servlet> <servlet-name>xfire</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>xfire</servlet-name> <url-pattern>*.ws</url-pattern> </servlet-mapping> <servlet> <!-- 配合Spring容器中XFire一起工作的Servlet--> <servlet-name>xfireServlet</servlet-name> <servlet-class>org.codehaus.xfire.spring.XFireSpringServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>xfireServlet</servlet-name> <!-- 在这个URI下开放Web Service服务 --> <url-pattern>/service/*</url-pattern> </servlet-mapping> <!-- end XFire 配置 --> </web-app>
2)Web Service的接口类HelloWorld.java和对应实现类HelloWorldImpl.java
为了用Web Service完成HelloWorld功能,我们首先在src/webservice目录下建立接口类HelloWold.java。它仅包含一个sayHelloWorld(String name)的方法,其详细内容如下:

package webservice; /** * HelloWorld的接口类. */ public interface HelloWorld { /** * 对名字为name的人打招呼. * @param name 名字 * @return 返回打招呼的字符串 */ String sayHelloWorld(String name); }
我们还需要建立一个对应的实现类,来实现sayHelloWorld的功能,该实现类即为HelloWorldImpl.java。该类的详细内容如下:


package webservice; /** * HelloWorld的实现类. */ public class HelloWorldImpl implements HelloWorld { public String sayHelloWorld(String name) { String helloWorld = "hello," + name; return helloWorld; } }
3)Spring配置文件applicationContext.xmlxfire-servlet.xml的配置
首先我们在applicationContext.xml文件中配置对应的bean——HelloWorldBean,该xml文件的内容如下:

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="HelloWorldBean" class="webservice.HelloWorldImpl"/> </beans>
这个配置文件很简单,在此不详述。
XFire为Spring提供了方便易用的导出器XFireExporter,借助该导出器的支持,我们可以在Spring容器中将一个POJO导出为Web Service。HelloWorld是业务服务类,在此拥有一个sayHelloWorld的方法,我们希望将此方法开放为Web Service。在实际应用中,如果某个类具有众多的方法,而其中的某些方法不需要开放为Web Service的情况下,我们可以定义一个窄接口,该接口中只需定义那些开放为Web Service的业务方法。
将一个业务类所有需要开放为Web Service的方法通过一个窄接口来描述是值得推荐的作法,这让Web Service的接口显得很“干净”。其次,XFire的导出器也需要服务接口的支持,因为它采用基于接口的动态代理技术。
窄接口中的方法在真实的系统中可能需要引用其它的业务类或DAO获取数据库中的真实数据,为了简化实例,我们在此简化了实例。
下面让我们看看在xfire-servlet.xml文件中导出器的设置,该文件内容如下:

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <!-- 引入XFire预配置信息 --> <import resource="classpath:org/codehaus/xfire/spring/xfire.xml" /> <!—定义访问的url--> <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="urlMap"> <map> <entry key="/HelloWorldService.ws"> <ref bean="HelloWorldService" /> </entry> </map> </property> </bean> <!-- 使用XFire导出器 --> <bean id="baseWebService" class="org.codehaus.xfire.spring.remoting.XFireExporter" lazy-init="false" abstract="true"> <!-- 引用xfire.xml中定义的工厂 --> <property name="serviceFactory" ref="xfire.serviceFactory" /> <!-- 引用xfire.xml中的xfire实例 --> <property name="xfire" ref="xfire" /> </bean> <bean id="HelloWorldService" parent="baseWebService"> <!-- 业务服务bean --> <property name="serviceBean" ref="HelloWorldBean" /> <!-- 业务服务bean的窄接口类 --> <property name="serviceClass" value="webservice.HelloWorld" /> </bean> </beans>
在上面的配置中,我们可以看到,在该配置文件中引入了xfire.xml这个Spring配置文件。它是在XFire核心JAR包中拥有一个预定义的Spring配置文件,它定义了XFire在Spring中必须用到的一些Bean和资源,需要引入这个预定义的配置文件。从该配置文件中可以看出,我们通过XFireExporter将业务类导出为Web Service,对于任何导出器,我们都需要引入XFire环境,即serviceFactoryxfire,这是标准的配置。ServiceFactory是XFire的核心类,它可以将一个POJO生成为一个Web Service。
在本实例中,我们通过定义一个baseWebService,其余的webService配置都将该bean作为父bean,这样可以简化Spring的配置,不需要多次引入serviceFactory和xfire。
0
相关文章