Description
1. Use the script language to allow JAVA to execute dynamic code
2. You can use Spring to proxy scripting languages into Java interface implementation classes.
3. Spring2.5.6 supports three scripting languages: ruby, Groovy, and BeanShell.
4. Use spring and beanshell In the example
5. Dependency spring2.5.6, bsh-2.0b4
Copy codeThe Code is as follows:
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 );
}