Implement the java interface example with beanshell via spring

  • 2020-09-28 09:05:59
  • OfStack

instructions

1. Let JAVA execute dynamic code through scripting language
2. With Spring, you can proxy the scripting language into the implementation class of the Java interface
3. Spring2.5.6 Supports three scripting languages, ruby,Groovy and BeanShell
4. In the example, spring combines with beanshell
5. Rely on spring2. 5.6, 2.0 b4 bsh -


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


Related articles: