Spring中使用Groovy动态Bean

转载自[url]http://blog.chenlb.com/2009/04/spring-use-groovy-dynamic-bean.html[/url]

在 spring 中使用 groovy 等动态语言的好处就是:在服务器上改改或新加个 groovy 文件就可以有新的功能,不用重新打包并部署。对一些规则性的逻辑处理、动态性强的应用可以 groovy。

示例下在 spring 中使用 groovy,我机子环境 spring 2.5.5, groovy-1.5.7。

1、模拟业务接口:


package com.chenlb.groovy;

/**
* 业务模拟接口
*
* @author chenlb 2009-4-7 下午02:04:38
*/
public interface Hello {

String say(String name);
}


2、groovy 实现:


package com.chenlb.groovy;

class GroovyHello implements Hello {

def blog;

String say(String name) {
return "Hello ${name}@${blog}"
}

void setUrl(String blog) {
this.blog = blog;
}
}

3、spring 配置 applicationContext-groovy.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-2.5.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-2.5.xsd">

<lang:groovy id="groovyHello" script-source="classpath:com/chenlb/groovy/GroovyHello.groovy">
<lang:property name="blog" value="http://blog.chenlb.com"></lang:property>
</lang:groovy>
</beans>




<?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-2.5.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-2.5.xsd">

<lang:groovy id="groovyHello" script-source="classpath:com/chenlb/groovy/GroovyHello.groovy">
<lang:property name="blog" value="http://blog.chenlb.com"></lang:property>
</lang:groovy>
</beans>


4、运行:

package com.chenlb.groovy;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class Main {

public static void main(String[] args) {
ApplicationContext context = new FileSystemXmlApplicationContext("classpath:applicationContext-groovy.xml");

Hello hello = (Hello) context.getBean("groovyHello");

System.out.println(hello.say("chenlb"));
}

}

package com.chenlb.groovy;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class Main {

public static void main(String[] args) {
ApplicationContext context = new FileSystemXmlApplicationContext("classpath:applicationContext-groovy.xml");

Hello hello = (Hello) context.getBean("groovyHello");

System.out.println(hello.say("chenlb"));
}

}


结果:

[b]Hello chenlb@http://blog.chenlb.com[/b]

要在 spring 中用 groovy,依赖的jar有:antlr-2.7.6.jar、aopalliance.jar、asm-2.2.3.jar、asm-commons-2.2.3.jar、asm-util-2.2.3.jar、groovy-1.5.5.jar在spring 的 lib中可以找到,还有spring-aop-2.5.5.jar。当然也可以把groovy-1.5.5.jar换成groovy-1.5.7.jar
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值