Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the methods for Java to execute groovy scripts

2025-04-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

这篇文章主要介绍"Java执行groovy脚本的方法有哪些",在日常操作中,相信很多人在Java执行groovy脚本的方法有哪些问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"Java执行groovy脚本的方法有哪些"的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

Java执行groovy脚本的两种方式:

一种是通过脚本引擎ScriptEngine提供的eval(String)方法执行脚本内容;一种是执行groovy脚本;

二者都通过Invocable来传递参数并获取执行结果;

Invocable:脚本引擎的解释器接口,提供invokeFunction和invokeMethod两种传递参数并获取执行结果的方法

以下为案例:

引入依赖

org.codehaus.groovy groovy-all 1.2.74

定义脚本内容并执行

public void testByFunction(){ // 初始化Bindings Bindings bindings = engine.createBindings(); // 绑定参数 bindings.put("date", new Date()); final String name = "groovy"; // 定义groovy脚本中执行方法的名称 final String scriptName = "execute"; // 定义groovy脚本内容 final String scriptContent = "def " + scriptName +"(name){" + " println("now dateTime is: ${date.getTime()}");" + " println("my name is $name");" + " return date.getTime() > 0;" + "}"; try { // 执行脚本 engine.eval(scriptContent, bindings); // 获取执行结果 Invocable invocable = (Invocable) engine; Boolean flag = (Boolean) invocable.invokeFunction(scriptName, name); System.out.println("---------------------------------------"); System.out.println("result is: " + flag); } catch (ScriptException | NoSuchMethodException e) { e.printStackTrace(); }}

运行结果:

invokeFunction方法的第一个参数为脚本的函数名称,把scriptName拎出来通过创建String对象再赋值进去,方便你看懂函数名称到底是哪个;

scriptContent中${date.getTime()}与$name的意思一样,grovvy中的字符串可以识别${}和$占位符;

bindings绑定参数与invokeFunction方法的第二个参数的区别是,前者是脚本内全局的,而后者是定义在函数内的;

实例化脚本对象并执行

public void testByMethod(){ try { // 初始化groovy脚本对象 final TestGroovy testGroovy = new TestGroovy(); // 定义groovy脚本中执行方法的名称 final String scriptName = "execute"; // 定义参数 final Date arg_1 = new Date(); final String arg_2 = "groovy"; // 执行脚本并获取结果 Invocable invocable = (Invocable) engine; Boolean flag = (Boolean) invocable.invokeMethod(testGroovy, scriptName, arg_1, arg_2); System.out.println("---------------------------------------"); System.out.println("result is: " + flag); } catch (ScriptException |NoSuchMethodException e) { e.printStackTrace(); }}

TestGroovy.groovy脚本内容:

package com.dandelion.groovyclass TestGroovy { static def execute(Date date, String name){ println("now dateTime is: ${date.getTime()}"); println("my name is $name"); return date.getTime()

< 0; }} invokeMethod方法的第一个参数是脚本对象,第二个参数是脚本中的函数名称,之后为绑定的参数; 源码: package com.dandelion.test;import com.dandelion.groovy.TestGroovy;import javax.script.*;import java.util.Date;public class TestScriptEngine { // 查找并创建指定脚本引擎 private ScriptEngine engine = new ScriptEngineManager().getEngineByName("groovy"); public void testByFunction(){ // 初始化Bindings Bindings bindings = engine.createBindings(); // 绑定参数 bindings.put("date", new Date()); // 定义groovy脚本中执行方法的名称 final String scriptName = "execute"; // 定义groovy脚本内容 final String scriptContent = "def " + scriptName +"(){" + " println("now dateTime is: ${date.getTime()}");" + " return date.getTime() >

0;" + "}"; try { //execute script engine.eval(scriptContent, bindings); //Get execution results Invocable invocable = (Invocable) engine; Boolean flag = (Boolean) invocable.invokeFunction(scriptName); System.out.println("---------------------------------------"); System.out.println("result is: " + flag); } catch (ScriptException | NoSuchMethodException e) { e.printStackTrace(); } } public void testByMethod(){ try { //initialize groovy script object final TestGroovy testGroovy = new TestGroovy(); //Define the name of the method executed in groovy script final String scriptName = "execute"; //define parameters final Date arg_1 = new Date(); final String arg_2 = "groovy"; //Execute script and get results Invocable invocable = (Invocable) engine; Boolean flag = (Boolean) invocable.invokeMethod(testGroovy, scriptName, arg_1, arg_2); System.out.println("---------------------------------------"); System.out.println("result is: " + flag); } catch (ScriptException |NoSuchMethodException e) { e.printStackTrace(); } } public static void main(String[] args) { TestScriptEngine engine = new TestScriptEngine(); engine.testByFunction(); }} At this point, the study of "What are the methods for Java to execute groovy scripts" is over, hoping to solve everyone's doubts. Theory and practice can better match to help everyone learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report