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、学会分析运行时候报错的异常

 

 

原创粉丝点击