作者:江南白衣
當王家衛遇到杜可風,Spring遇到Ruby。想出這個題目的Crraig Walls 絕對也是個八卦種子,宣傳的是Spring 2.整合動態語言的feature。
此Feature目的是讓動態語言編寫的類,可以良好的融入java/spring的大架構內,洗白成application context中的普通一員,擁有與其他pojo良好的互動與注入能力,可以被DWR,XFire,Quartz匯出成各式服務。
很簡單,SpringSide 裡的/springside-bookstore/src/.../plugins/webservice/applicationContext-webservice-client.xml檔案已說明了一切用法。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:lang="http://www.springframework.org/schema/lang"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd">
<!--Groovy版本的Web Service Client-->
<lang:groovy id="BookServiceClient"
refresh-check-delay="60000"
script-source="classpath:org/springside/bookstore/plugins/webservice/service/BookServiceClient.groovy">
<lang:property name="serviceUrl" value="http://localhost:8080/springside/service/BookService"/>
</lang:groovy>
</beans>
注意spring 2.0為語言整合提供了<lang:language>的簡寫法,詳細見Spring的參考文檔第24章
第一個屬性 refresh-check-delay,每隔一段時間掃描一次指令碼的變化,reload it。
第二個屬性script-source 指向scriptsouce, <lang:property>演繹了Spring最喜愛的IOC,將serviceUr變數注入。
當然,世上沒有奇蹟,Spring 與Groovy、JRuby 是靠一個Interface來定義連接點的。Spring使用BookServiceClient Bean 時,實際使用的是BookService介面。 Groovy因為是Java近親,所以可以在類裡面直接implements BookService介面,而JRuby則必須把interface作為構造參數傳給RubyScriptFactory。這樣有個不爽的地方,在Groovy裡必須顯式定義介面的所有函數和參數、傳回值的類型。Ruby的Miss Method與Groovy的MOP反射頓時沒了用武之地。
舞台已經搭好,界限已經模糊,接下來就和AOP一樣,看大家的想象力了。