通过spring用beanshell实现java接口示例

说明

1.通过脚本语言让JAVA执行动态代码
2.用Spring可以将脚本语言代理成Java接口的实现类
3.Spring2.5.6中支持三种脚本语言ruby,Groovy,BeanShell
4.示例中为spring与beanshell结合
5.依赖spring2.5.6,bsh-2.0b4


import org.junit.Test;
import org.springframework.scripting.bsh.BshScriptUtils;

import bsh.EvalError;

public class TestBeanShell {
 @Test
 public void testShell()  {
  String srciptText = "say(name){ return \"hello,\"+name;}";
  SayHello sh;
  try {
   sh = (SayHello) BshScriptUtils.createBshObject(srciptText,new Class[] { SayHello.class });
   String res=sh.say("vidy");
   System.out.println(res);
  } catch (EvalError e) {
   // Todo Auto-generated catch block
   e.printstacktrace();
  }

 }
}
interface SayHello {
 public String say(String name);
}

相关文章

HashMap是Java中最常用的集合类框架,也是Java语言中非常典型...
在EffectiveJava中的第 36条中建议 用 EnumSet 替代位字段,...
介绍 注解是JDK1.5版本开始引入的一个特性,用于对代码进行说...
介绍 LinkedList同时实现了List接口和Deque接口,也就是说它...
介绍 TreeSet和TreeMap在Java里有着相同的实现,前者仅仅是对...
HashMap为什么线程不安全 put的不安全 由于多线程对HashMap进...