[webservices開發(fā)]XFire在SpringSide中的應(yīng)用
Posted on 2008-03-03 11:25 semovy 閱讀(320) 評(píng)論(0) 編輯 收藏 所屬分類: WebServiceSpringSide開源項(xiàng)目是國(guó)內(nèi)的開發(fā)人員所做的一個(gè)以Spring為核心的開源項(xiàng)目,目的是提供一個(gè)Pragmatic的企業(yè)應(yīng)用開發(fā)基礎(chǔ)和最佳實(shí)踐展示。為使用Spring框架的開發(fā)者提供一個(gè)非Demo版的復(fù)雜、正式而體現(xiàn)最佳使用實(shí)踐的參照系統(tǒng)。為JavaEEer必須面對(duì)的所有問題提供合理的、合乎Pragmatic原則的解決方案。采用Plugins形式組織,使開發(fā)者可快速定位所需的參考方案并做加法到自己的系統(tǒng)。
SpringSide中關(guān)于Web服務(wù)的配置是在
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參考手冊(cè)(2.0-M3)3.18節(jié)中有介紹,3.19節(jié)介紹了在web.xml配置bean定義文件的方法,就是上一節(jié)使用的方法。
第二步,處理映射,將/BookService URL和bookService這個(gè)bean聯(lián)系起來。當(dāng)然,這里bookService又繼承了baseWebService,這樣做挺巧妙,這樣如果有多個(gè)Web服務(wù)bean,就繼承baseWebService就可以了,這種面向?qū)ο蟮母拍钸€是值得我們提倡的,Spring參考手冊(cè)3.5節(jié)介紹了相關(guān)的知識(shí)。
在bookService的定義中,serviceBean也就是接口實(shí)現(xiàn)類為bookManager bean,這個(gè)bean實(shí)際是在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(); } |
事實(shí)上,SpringSide既是一個(gè)Web服務(wù)的提供者,又是一個(gè)Web服務(wù)的消費(fèi)者。它在WEB-INF/classes/applicationContext-webservice-client.xml文件中定義了消費(fèi)這個(gè)Web服務(wù)的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> |
這個(gè)消費(fèi)Web服務(wù)的bean定義為BookServiceClient,它是采用Groovy腳本語言定義了。在Spring參考手冊(cè)(2.0-M3)中的第25章專門介紹在Spring中腳本語言的使用,腳本語言支持是Spring 2.0新增的內(nèi)容,目前可以支持Groovy、BeanShell、Jruby三種腳本語言。
這個(gè)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中的應(yīng)用就介紹到這里為止。