这一节,通过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中的应用就介绍到这里为止。
闽公网安备 35060202000074号