使用XFire+Spring构建Web Service(1)
<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的接口类.
?*/
publicinterface?HelloWorld?{
????/**
?????*对名字为name的人打招呼.
?????*@paramname名字
?????*@return返回打招呼的字符串
?????*/
????String?sayHelloWorld(String?name);
}
我们还需要建立一个对应的实现类,来实现sayHelloWorld的功能,该实现类即为HelloWorldImpl.java。该类的详细内容如下:
package?webservice;
/**
?*HelloWorld的实现类.
?*/
publicclass?HelloWorldImpl?implements?HelloWorld?{
????public?String?sayHelloWorld(String?name)?{
???????String?helloWorld?=?"hello,"?+?name;
???????return?helloWorld;
????}
}
3)Spring配置文件applicationContext.xml和xfire-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环境,即serviceFactory和xfire,这是标准的配置。ServiceFactory是XFire的核心类,它可以将一个POJO生成为一个Web Service。
在本实例中,我们通过定义一个baseWebService,其余的webService配置都将该bean作为父bean,这样可以简化Spring的配置,不需要多次引入serviceFactory和xfire。
?