在java中使用groovy怎么搞
1个回答
展开全部
1、静态编译,在java工程中直接写groovy的文件,然后可以在groovy的文件中引用java工程的类,这种方式能够有效的利用groovy自身的语言特性,例如闭包;
2、通过groovyShell类直接执行脚本,例如:
12345678910111213package groovy_dsl.shell;import groovy.lang.Binding;import groovy.lang.GroovyShell;public class GroovyShellEx { public static void main(String[] args) { Binding bind = new Binding(); bind.setVariable("name", "iamzhongyong"); bind.setVariable("age", "25"); GroovyShell shell = new GroovyShell(bind); Object obj = shell.evaluate("str = name+age;return str"); System.out.println(obj); }}
3、通过groovyScriptEngine执行文件或者脚本,例如:
123456789package groovy_dsl.script;import groovy.util.GroovyScriptEngine;public class ScriptEngine { public static void main(String[] args) throws Exception { GroovyScriptEngine engine = new GroovyScriptEngine(""); Object obj = engine.run("src/main/java/groovy_dsl/script/script_test.groovy", "iamzhongyong"); System.out.println(obj); }}
4、通过GroovyClassLoader来执行,例如:123456789101112131415161718192021222324package groovy_dsl.classloader;import groovy.lang.GroovyClassLoader;import groovy.lang.GroovyObject;import java.io.File;import java.io.IOException;public class GroovyClassLoaderEx { public static void main(String[] args) throws Exception, IOException { GroovyClassLoader loader = new GroovyClassLoader(); for(int i=0;i<100;i++){ Class<?> clazz = loader.parseClass(new File("src/main/java/groovy_dsl/classloader/UserDO.groovy")); GroovyObject clazzObj = (GroovyObject)clazz.newInstance(); clazzObj.invokeMethod("setName", "iamzhongyong"); clazzObj.invokeMethod("setSex", "Boy"); clazzObj.invokeMethod("setAge", "26"); System.out.println(clazzObj.invokeMethod("getAllInfo", null)); } }}
2、通过groovyShell类直接执行脚本,例如:
12345678910111213package groovy_dsl.shell;import groovy.lang.Binding;import groovy.lang.GroovyShell;public class GroovyShellEx { public static void main(String[] args) { Binding bind = new Binding(); bind.setVariable("name", "iamzhongyong"); bind.setVariable("age", "25"); GroovyShell shell = new GroovyShell(bind); Object obj = shell.evaluate("str = name+age;return str"); System.out.println(obj); }}
3、通过groovyScriptEngine执行文件或者脚本,例如:
123456789package groovy_dsl.script;import groovy.util.GroovyScriptEngine;public class ScriptEngine { public static void main(String[] args) throws Exception { GroovyScriptEngine engine = new GroovyScriptEngine(""); Object obj = engine.run("src/main/java/groovy_dsl/script/script_test.groovy", "iamzhongyong"); System.out.println(obj); }}
4、通过GroovyClassLoader来执行,例如:123456789101112131415161718192021222324package groovy_dsl.classloader;import groovy.lang.GroovyClassLoader;import groovy.lang.GroovyObject;import java.io.File;import java.io.IOException;public class GroovyClassLoaderEx { public static void main(String[] args) throws Exception, IOException { GroovyClassLoader loader = new GroovyClassLoader(); for(int i=0;i<100;i++){ Class<?> clazz = loader.parseClass(new File("src/main/java/groovy_dsl/classloader/UserDO.groovy")); GroovyObject clazzObj = (GroovyObject)clazz.newInstance(); clazzObj.invokeMethod("setName", "iamzhongyong"); clazzObj.invokeMethod("setSex", "Boy"); clazzObj.invokeMethod("setAge", "26"); System.out.println(clazzObj.invokeMethod("getAllInfo", null)); } }}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询