Implementation of java interface example with BeanShell through spring

Time:2020-10-26

explain

1. Let Java execute dynamic code through script language
2. The script language can be proxy to java interface implementation class with spring
3. Spring 2.5.6 supports three scripting languages ruby, groovy and BeanShell
4. In the example, spring is combined with BeanShell
5. Rely on spring 2.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);
}