作者:江南白衣?

??? 當(dāng)王家衛(wèi)遇到杜可風(fēng),Spring遇到Ruby。想出這個(gè)題目的Crraig Walls 絕對也是個(gè)八卦種子,宣傳的是Spring 2.集成動態(tài)語言的feature。
?????
???? 此Feature目的是讓動態(tài)語言編寫的類,可以良好的融入java/spring的大框架內(nèi),洗白成application context中的普通一員,擁有與其他pojo良好的交互與注入能力,可以被DWR,XFire,Quartz導(dǎo)出成各式服務(wù)。

????? 很簡單,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>的簡寫法,詳細(xì)見Spring的參考文檔第24章??

??? 第一個(gè)屬性 refresh-check-delay,每隔一段時(shí)間掃描一次腳本的變化,reload it。

?? 第二個(gè)屬性script-source 指向scriptsouce, <lang:property>演繹了Spring最喜愛的IOC,將serviceUr變量注入。

??? 當(dāng)然,世上沒有奇跡,Spring 與Groovy、JRuby 是靠一個(gè)Interface來定義連接點(diǎn)的。Spring使用BookServiceClient Bean 時(shí),實(shí)際使用的是BookService接口。?Groovy因?yàn)槭荍ava近親,所以可以在類里面直接implements BookService接口,而JRuby則必須把interface作為構(gòu)造參數(shù)傳給RubyScriptFactory。這樣有個(gè)不爽的地方,在Groovy里必須顯式定義接口的所有函數(shù)和參數(shù)、返回值的類型。Ruby的Miss Method與Groovy的MOP反射頓時(shí)沒了用武之地。
???? 舞臺已經(jīng)搭好,界限已經(jīng)模糊,接下來就和AOP一樣,看大家的想象力了。