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