groovy 三种运用

1、groovyShell 脚本语言运用,支持表达式及脚本文件:

def run(foo) {
       println 'Hello World!'
       x = 123
       foo * 10
}
run foo

 

public class TestGroovyShell {
 /**
  * @param args
  */
 public static void main(String[] args) {
  try {
   Binding binding = new Binding();
   binding.setProperty("foo", new Integer(2));
   GroovyShell shell = new GroovyShell(binding);
   
   Object value = shell.evaluate(new File("src/groovy/GroovyShellHellow.groovy"));
   System.out.println(value);
  } catch (CompilationFailedException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
    }
}

2、Java中动态加载和运行Groovy代码

  

package groovy;

/**
 * @author zhangchao02
 *
 */
public class talk{

 public String say(String talkContext) {
  System.out.println("my say:" + talkContext);
  return talkContext;
 }

}

public class TestGroovyShell {
 /**
  * @param args
  */
 public static void main(String[] args) {
  try {
   Binding binding = new Binding();
   binding.setProperty("foo", new Integer(2));
   GroovyShell shell = new GroovyShell(binding);
   
   Object value = shell.evaluate(new File("src/groovy/GroovyShellHellow.groovy"));
   System.out.println(value);
  } catch (CompilationFailedException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
    }
}

public class GroovyHelper {
    private static final Logger logger = LoggerFactory.getLogger(GroovyHelper.class);
 private static GroovyClassLoader loader;

 static {
  ClassLoader cl = new GroovyHelper().getClass()
    .getClassLoader();
  loader = new GroovyClassLoader(cl);
 }

 private GroovyHelper() {
 };

 public static GroovyObject getGroovyObject(String filePath) {
  File groovyFile = new File(filePath);
  if(!groovyFile.exists()){
   logger.error("GroovyHelper getGroovyObject groovyFile is not exists filePath={}", filePath);
   return null;
  }
  
  GroovyObject groovyObject = null;
  try {
   loader.parseClass(groovyFile);
   Class<?> groovyClass = loader.parseClass(groovyFile);
   groovyObject = (GroovyObject) groovyClass.newInstance();
  } catch (Exception e) {
   e.printStackTrace();
  }
  return groovyObject;
 }

 public static Object invokeMethod(GroovyObject groovyObject,
   String paramString, Object paramObject) {
  Object result = groovyObject.invokeMethod(paramString, paramObject);
  return result;
 }
 
 public static Object invokeMethodShell(){
  Object result = null;
  return result;
 }
}

3、groovy 脚本引擎:

 public static void main(String[] args) {
  String[] roots  =   new  String[]  {"src/groovy/"} ;
  try {
   GroovyScriptEngine gse  =   new  GroovyScriptEngine(roots);
   Binding binding = new Binding();
   binding.setProperty("foo", new Integer(2));
   gse.run( "GroovyShellHellow.groovy" , binding);
   System.out.println(binding.getVariable("x"));
   System.out.println(binding.getVariable("foo"));
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (ResourceException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (ScriptException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值