說明
1.通過腳本語言讓JAVA執(zhí)行動態(tài)代碼
2.用Spring可以將腳本語言代理成Java接口的實現(xiàn)類
3.Spring2.5.6中支持三種腳本語言ruby,Groovy,BeanShell
4.示例中為spring與beanshell結(jié)合
5.依賴spring2.5.6,bsh-2.0b4
復(fù)制代碼 代碼如下:
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);
}
新聞熱點
疑難解答