当前位置:首页 > JSP教程 > JSP常见问题

通过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);
}



【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!

相关教程推荐

其他课程推荐