这一节,通过SpringSide来分析XFire的应用。
?
SpringSide开源项目是国内的开发人员所做的一个以Spring为核心的开源项目,目的是提供一个Pragmatic的企业应用开发基础和最佳实践展示。为使用Spring框架的开发者提供一个非Demo版的复杂、正式而体现最佳使用实践的参照系统。为JavaEEer必须面对的所有问题提供合理的、合乎Pragmatic原则的解决方案。采用Plugins形式组织,使开发者可快速定位所需的参考方案并做加法到自己的系统。
?
SpringSide中关于Web服务的配置是在
WEB-IBF/classes文件下的applicationContext-webservice.xml中配置的:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> ??? <import resource="classpath:org/codehaus/xfire/spring/xfire.xml"/> ??? <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> ? ??????<property name="mappings"> ??????????? <value>/BookService=bookService</value> ??????? </property> ??? </bean> ? ??? <bean id="baseWebService" class="org.codehaus.xfire.spring.remoting.XFireExporter" abstract="true"> ??????? <property name="serviceFactory" ref="xfire.serviceFactory"/> ??????? <property name="xfire" ref="xfire"/> ??? </bean> ??? ??? <bean id="bookService" parent="baseWebService"> ??????? <property name="serviceBean" ref="bookManager"/> ??????? <property name="serviceClass" value="org.springside.bookstore.service.webservice.BookService"/> ??? </bean> </beans> |
?
第一步,引入xfire.xml文件
<import resource="classpath:org/codehaus/xfire/spring/xfire.xml"/>。这样,我们就不必在web.xml中配置了,这种语法在Spring参考手册(2.0-M3)3.18节中有介绍,3.19节介绍了在web.xml配置bean定义文件的方法,就是上一节使用的方法。
?
第二步,处理映射,将/BookService URL和bookService这个bean联系起来。当然,这里bookService又继承了baseWebService,这样做挺巧妙,这样如果有多个Web服务bean,就继承baseWebService就可以了,这种面向对象的概念还是值得我们提倡的,Spring参考手册3.5节介绍了相关的知识。
?
在bookService的定义中,serviceBean也就是接口实现类为bookManager bean,这个bean实际是在WEB-INF/classes/ applicationContext-manager.xml文件中所定义,类名为org.springside.bookstore.service.logic.BookManager:
package org.springside.bookstore.service.logic; ? import … … ? public class BookManager extends BaseHibernateDao implements BookService { ? ??? private CategoryManager categoryManager; ? ??? public void setCategoryManager(CategoryManager categoryManager) { ??????? this.categoryManager = categoryManager; ??? } ? ??? protected Class getEntityClass() { ??????? … … ??? } ? ??? public Book get(Integer id) { ??????? … … ??? } ? ??? public void save(Book book) { ?????? … … ??? } ? ??? public void remove(Integer id) { ?????? … … ??? } ? ??? public List getAllCategorys() { ??????? … … ??? } ? ??? public Category getCategory(Integer id) { ??????? … … ??? } ? ??? public List findBooksByName(String name) { ??????? … … ??? } ?? ??? public List getNewBooks() { ??????? … … ??? } ? ??? public List findAllLowStock(Integer stockHint) { ?????? … … ??? } ? ??? public List findBooksByCategory(String category_id) { ?????? … … ??? } ? ??? protected void filterCriteria(Criteria criteria,Map filter) { ?????? … … ??? } } |
?
serviceClass也就是接口类为
org.springside.bookstore.service.webservice.BookService:
package org.springside.bookstore.service.webservice; ? import java.util.List; ? public interface BookService { ??? List findBooksByName(String name); ?? ??? List findBooksByCategory(String category); ? ??? List getAllCategorys(); ? } |
?
事实上,SpringSide既是一个Web服务的提供者,又是一个Web服务的消费者。它在WEB-INF/classes/applicationContext-webservice-client.xml文件中定义了消费这个Web服务的bean:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> ??? <!-- 一分钟刷新一次sciprt文件--> ??? <bean class="org.springframework.scripting.support.ScriptFactoryPostProcessor"> ????????? <property name="refreshCheckDelay" value="60" /> ??? </bean> ? ??? <bean id="BookServiceClient" class="org.springframework.scripting.groovy.GroovyScriptFactory"> ???????? <constructor-arg value="classpath:org/springside/bookstore/service/webservice/BookServiceClient.groovy"/> ???????? <property name="serviceUrl" value="http://localhost:8080/springside/service/BookService" /> ??? </bean> </beans> |
?
这个消费Web服务的bean定义为BookServiceClient,它是采用Groovy脚本语言定义了。在Spring参考手册(2.0-M3)中的第25章专门介绍在Spring中脚本语言的使用,脚本语言支持是Spring 2.0新增的内容,目前可以支持Groovy、BeanShell、Jruby三种脚本语言。
这个BookServiceClient最终是在dwr中使用,可以
plugins/org.springside.ajax.dwr/webapp/WEB-INF/dwr.xml中的定义。
?
在SpringSide,采用Aegis的binding方式,在
plugins/org.springside.webservice.xfire/src/org/springside/bookstore/service/webservice/BookService.aegis.xml中定义了返回值的类型:
<?xml version="1.0" encoding="UTF-8"?> <mappings> ??? <mapping> ??????? <method name="findBooksByName"> ??????????? <return-type componentType="org.springside.bookstore.domain.Book"/> ??????? </method> ???? ???<method name="findBooksByCategory"> ??????????? <return-type componentType="org.springside.bookstore.domain.Book"/> ??????? </method> ??????? <method name="getAllCategorys"> ??????????? <return-type componentType="org.springside.bookstore.domain.Category"/> ??????? </method> ??? </mapping> </mappings> |
?
?
XFire在SpringSide中的应用就介绍到这里为止。