[webservices開發]XFire在SpringSide中的應用
Posted on 2008-03-03 11:25 semovy 閱讀(313) 評論(0) 編輯 收藏 所屬分類: WebServiceSpringSide開源項目是國內的開發人員所做的一個以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中的應用就介紹到這里為止。