Java执行groovy脚本的方法有哪些

发布时间:2022-02-24 17:55:10 作者:iii
来源:亿速云 阅读:212

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

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

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

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

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

以下为案例:

引入依赖

<dependency>
	<groupId>org.codehaus.groovy</groupId>
	<artifactId>groovy-all</artifactId>
	<version>1.2.74</version>
</dependency>

定义脚本内容并执行

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();
    }
}

运行结果:

Java执行groovy脚本的方法有哪些

实例化脚本对象并执行

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.groovy

class 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 {
            // 执行脚本
            engine.eval(scriptContent, bindings);
            // 获取执行结果
            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 {
            // 初始化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();
        }
    }

    public static void main(String[] args) {
        TestScriptEngine engine = new TestScriptEngine();
        engine.testByFunction();
    }
}

到此,关于“Java执行groovy脚本的方法有哪些”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

推荐阅读:
  1. 执行php脚本的方法有哪些
  2. java中调用groovy脚本

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

java groovy

上一篇:怎么解决Tkinter中button按钮未按却主动执行command函数

下一篇:javascript如何将小数转化为百分数

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》