说明
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);
}