spring和3大动态语言(jruby groovy beanshell)的例子
来源:互联网 发布:适合学生的淘宝服装店 编辑:程序博客网 时间:2024/04/28 07:25
今天学习和实践了spring对动态语言的支持。步骤如下:
1、JAVA的bean(IdoSoft .java)
package ids.spring.jruby;
public interface IdoSoft {
String getInfo();
//void setInfo(String aInfo);
}
2、动态语言
1)、jruby的实现代码(RubyIdoSoft.rb)
require 'java'
include_class 'ids.spring.jruby.IdoSoft'
class RubyIdoSoft //千万不要用"< IdoSoft"来实现接口,否则报错
def setInfo(aInfo)
@@info = aInfo
end
def getInfo
@@info
end
end
RubyIdoSoft.new
//千万不要用"< IdoSoft"来实现接口,否则报错
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'idososft': BeanPostProcessor before instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scriptedObject.idososft': Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public java.lang.Object org.springframework.scripting.jruby.JRubyScriptFactory.getScriptedObject(org.springframework.scripting.ScriptSource,java.lang.Class[]) throws java.io.IOException,org.springframework.scripting.ScriptCompilationException] threw exception; nested exception is org.springframework.scripting.ScriptCompilationException: Could not compile script [class path resource [ids/spring/jruby/RubyIdoSoft.rb]]: superclass must be a Class (Module) given; nested exception is org.jruby.exceptions.RaiseException: superclass must be a Class (Module) given
2)、grooby的实现代码(GroovyIdoSoft.groovy)
package ids.spring.jruby;
import ids.spring.jruby.IdoSoft;
class GroovyIdoSoft implements IdoSoft{
def info;
public void setInfo(String info){
this.info = info;
}
public String getInfo(){
return info;
}
}
3)、beansheel的实现代码(BshIdoSoft.rb)
String info;
String getInfo(){
return info;
}
void setInfo(String aInfo){
info = aInfo ;
}
3、需要jar 文件
jruby.jar //jruby需要的
cglib-nodep.jar //代理需要
backport-util-concurrent.jar
asm.jar,antlr.jar.groovy.jar
bsh-2.0b4.jar
4、bean配置文件AppContext.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:tx="http://www.springframework.org/schema/tx"
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.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/lang
http://www.springframework.org/schema/lang/spring-lang-2.0.xsd">
<lang:jruby id="idosoft"
script-interfaces="ids.spring.jruby.IdoSoft"
script-source="classpath:ids/spring/jruby/RubyIdoSoft.rb">
<lang:property name="info" value="Hello,This is Jruby Dynamic Language"/>
</lang:jruby>
<lang:groovy id="idosoft1"
script-source="classpath:ids/spring/jruby/GroovyIdoSoft.groovy">
<lang:property name="info" value="Hello,This is Groovy Dynamic Language"/>
</lang:groovy>
<lang:bsh id="idosoft2"
script-source="classpath:ids/spring/jruby/BshIdoSoft.bsh"
script-interfaces="ids.spring.jruby.IdoSoft">
<lang:property name="info" value="Hello,This is bsh Dynamic Language"/>
</lang:bsh>
</beans>
5、测试程序
package ids.spring.test;
import ids.spring.jruby.IdoSoft;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/*
* crate by idosoft.com.cn
*/
public class TestRuby {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String config="AppContext.xml";
ApplicationContext ctx = new ClassPathXmlApplicationContext(config);
IdoSoft idosoft = (IdoSoft)ctx.getBean("idosoft");
System.out.println("Result="+idosoft.getInfo());
IdoSoft idosoft1 = (IdoSoft)ctx.getBean("idosoft1");
System.out.println("Result="+idosoft1.getInfo());
IdoSoft idosoft2 = (IdoSoft)ctx.getBean("idosoft2");
System.out.println("Result="+idosoft2.getInfo());
}
}
6、总结
a、书写代码时候注意名称前后保持一致
b、在jruby中实现接口,千万不要用"< IdoSoft"来实现接口,否则报错
c、bean配置中的名称和测试类中引用的名称一致
d、学会分析运行时候报错的异常
- spring和3大动态语言(jruby groovy beanshell)的例子
- spring动态执行语言beanshell
- Spring 2.0+ JRuby 快速入门四(Spring对动态语言的支持)
- Jython、JRuby 和 Groovy
- 10048---【JMeter】Groovy和BeanShell脚本的性能比较
- Groovy语言的安装和Hello World例子
- Scala, Groovy, Clojure, Jython, JRuby and Java ----我们的工作语言
- JRuby、Rhino、Groovy和Jython性能及内存消耗的简单对比
- Groovy还是Jruby?
- 请放弃 Jruby和Jython 而选择 Groovy
- Sun 收购JRuby 向动态语言迈进
- Groovy动态语言
- groovy动态语言
- Spring与Groovy动态语言集成包冲突问题
- java调用脚本语言笔记(jython,jruby,groovy)
- groovy的简单例子
- Compare JRuby, Groovy and Python
- JRuby语言内部报道:CRuby语言和JRuby语言gem命令差异
- 请求担任独家代理
- BeanShell简介
- 湖南妹类细急
- sdfsdf
- 《JAVA编程思想》第四版学习 需要我记住的something –持有对象
- spring和3大动态语言(jruby groovy beanshell)的例子
- spring利用javamail,quartz定时发送邮件
- 微软实习生考核(1)
- 正则表达式引擎
- 使用xfire+spring编写webservice例子
- 获取进程的开始、结束时间
- JPA学习笔记
- 监视工具的过滤功能
- STUDY