java interface
package groovy;
public interface IFoo {
public Object run(Object foo, Object bar);
}
groovy implements
package groovy;
class Foo implements IFoo {
def x
public Object run(Object foo, Object bar) {
println 'Hello World!'
x = 123
println foo * 10
println bar
return 'success'
}
}
java call groovy
package groovy;
import groovy.lang.GroovyClassLoader;
import groovy.lang.GroovyObject;
import java.io.File;
public class InvokeGroovy {
public static void main(String[] args) {
ClassLoader cl = new InvokeGroovy().getClass().getClassLoader();
GroovyClassLoader groovyCl = new GroovyClassLoader(cl);
try {
@SuppressWarnings("rawtypes")
Class groovyClass = groovyCl.parseClass(new File(InvokeGroovy.class.getResource("/groovy/Foo.groovy").toURI()));
IFoo foo = (IFoo) groovyClass.newInstance();
System.out.println(foo.run(new Integer(2), "More parameter..."));
System.out.println("=============================");
GroovyObject groovyObject = (GroovyObject) groovyClass.newInstance();
System.out.println(groovyObject.invokeMethod("run", new Object[]{new Integer(2),"More parameter..."}));
} catch (Exception e) {
e.printStackTrace();
}
}
}
java execute groovy script
package groovy;
import groovy.lang.Binding;
import groovy.lang.GroovyShell;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
public class TestEval {
public Object doit() {
ScriptEngineManager factory = new ScriptEngineManager(TestEval.class.getClassLoader());
ScriptEngine scriptEngine = factory.getEngineByName("groovy");//或者"Groovy" groovy版本要1.6以上的
try {
scriptEngine.put("test", "hello world!");
scriptEngine.put("outer", this);
scriptEngine.eval("println test; outer.java_out()");
} catch (ScriptException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Binding bb = new Binding();
bb.setVariable("test", "hello world!");
bb.setProperty("outer", this);
GroovyShell gs = new GroovyShell(bb);
return gs.evaluate("println test; outer.java_out()");
}
/**
* @param args
*/
public static void main(String[] args) {
TestEval te = new TestEval();
te.doit();
}
public void java_out(){
System.out.println("out from java");
}
}
分享到:
相关推荐
Groovy 调用 Java 类groovy 调用 Java class 十分方便,只需要在类前导入该 Java 类,在 Groovy 代码中就可以无缝使用该
}}编写一个 Groovy 类调用以上的 Java Beanclass GroovyCallJava { static void main(args) { Ja
Java调用Groovy,实时动态加载数据库groovy脚本,java读取mongoDB的groovy脚本,加载实时运行,热部署
Java中使用Groovy的三种方式,详细见我的博客。
Spock 是适合 Java 和 Groovy 应用程序的一个测试框架
干货:Jenkins Pipeline调用shell、python、java、groovy脚本的正确使用姿势.doc
Making Java Groovy is a practical handbook for developers who want to blend Groovy into their day to day work with Java It starts by introducing the key differences between Java and Groovy and how you...
SpringBoot-Gradle-Maven-Java-Groovy:SpringBoot通过不同构建工具和语言实现。
NULL 博文链接:https://yangwencan2002.iteye.com/blog/260697
概述主要介绍Java、spring与groovy结合使用,高清英文版本
脚本通过上下文applicationcontext获取被测试应用bean,从而可以进行接口内容获取等操作。目前该方式主要运用到性能平台脚本搭建、代码深度测试、接口测试、白盒测试等多方面。...支持Java、groovy脚本。
java嵌入groovy所需jar 2.3.7最新版
Making Java Groovy Kenneth A. Kousen 1.Easier Java 2.Closures, builders, and metaprogramming 3.Gradle for builds, Spock for testing 4.Groovy frameworks like Grails and Griffon 源码
groovy-se-study 本工程用于学习groovy和java-se的调用
Groovy环境搭建教程中的例子工程,纯Java、纯Groovy以及Java+Groovy混合 教程参考:http://blog.csdn.net/rcom10002/archive/2011/06/26/6568557.aspx
Making Java Groovy
Making Java Groovy--Kenneth.A.Kousen. Java Groovy 很不错的学习资料
NULL 博文链接:https://yinxvxv.iteye.com/blog/811328
java 动态脚本语言 精通 Groovy
JGSK, Java,Groovy,Scala,Kotlin 四种语言的特点对比