作者:
江南白衣?
??? 當王家衛遇到杜可風,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一樣,看大家的想象力了。