Groovy

Author: 江南白衣

网站: http://groovy.codehaus.org

1.与Spring集成

Spring 2.0的Script Language集成,是让动态语言编写的类,可以良好的融入java/spring的大框架内,洗白成application context中的普通一员,拥有与其他pojo良好的交互与注入能力。

      很简单,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一样,看大家的想象力了。