App下載

關于Java執(zhí)行groovy腳本的兩種方法詳細解析

逃避全世界 2021-08-09 16:33:32 瀏覽數 (6233)
反饋

groovy是基于Java虛擬機的一門敏捷開發(fā)語言,能夠很好地和Java語言相結合,也可以用來擴展現有的代碼。下面,將為大家介紹關于Java執(zhí)行groovy腳本的兩種方法。

記錄Java執(zhí)行groovy腳本的兩種方式,簡單粗暴:

一種是通過腳本引擎ScriptEngine提供的eval(String)方法執(zhí)行腳本內容;一種是執(zhí)行groovy腳本;

二者都通過Invocable來傳遞參數并獲取執(zhí)行結果;

Invocable:腳本引擎的解釋器接口,提供invokeFunctioninvokeMethod兩種傳遞參數并獲取執(zhí)行結果的方法,Java JDK API文檔解釋如下:

invokeFunction:

invokeMethod:

以下為案例:

引入依賴

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

定義腳本內容并執(zhí)行

public void testByFunction(){
    // 初始化Bindings
    Bindings bindings = engine.createBindings();
    // 綁定參數
    bindings.put("date", new Date());
    final String name = "groovy";
    // 定義groovy腳本中執(zhí)行方法的名稱
    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 {
        // 執(zhí)行腳本
        engine.eval(scriptContent, bindings);
        // 獲取執(zhí)行結果
        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拎出來通過創(chuàng)建String對象再賦值進去,方便你看懂函數名稱到底是哪個;
  • scriptContent${date.getTime()}$name的意思一樣,grovvy中的字符串可以識別${}$占位符;
  • bindings綁定參數與invokeFunction方法的第二個參數的區(qū)別是,前者是腳本內全局的,而后者是定義在函數內的;

例如把腳本內容定義為這樣:

執(zhí)行結果就是這樣了:

實例化腳本對象并執(zhí)行

public void testByMethod(){
    try {
        // 初始化groovy腳本對象
        final TestGroovy testGroovy = new TestGroovy();
        // 定義groovy腳本中執(zhí)行方法的名稱
        final String scriptName = "execute";
        // 定義參數
        final Date arg_1 = new Date();
        final String arg_2 = "groovy";
        // 執(zhí)行腳本并獲取結果
        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 {

    // 查找并創(chuàng)建指定腳本引擎
    private ScriptEngine engine = new ScriptEngineManager().getEngineByName("groovy");

    public void testByFunction(){
        // 初始化Bindings
        Bindings bindings = engine.createBindings();
        // 綁定參數
        bindings.put("date", new Date());
        // 定義groovy腳本中執(zhí)行方法的名稱
        final String scriptName = "execute";
        // 定義groovy腳本內容
        final String scriptContent = "def " + scriptName +"(){" +
                                     "    println("now dateTime is: ${date.getTime()}");" +
                                     "    return date.getTime() > 0;" +
                                     "}";
        try {
            // 執(zhí)行腳本
            engine.eval(scriptContent, bindings);
            // 獲取執(zhí)行結果
            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腳本中執(zhí)行方法的名稱
            final String scriptName = "execute";
            // 定義參數
            final Date arg_1 = new Date();
            final String arg_2 = "groovy";
            // 執(zhí)行腳本并獲取結果
            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執(zhí)行groovy腳本的兩種方式和詳細代碼的全部內容,想要了解更多相關Java groovy腳本內容,請搜索W3Cschool以前的文章或繼續(xù)瀏覽下面的相關文章,同時也希望本篇文章能夠對大家的學習有所幫助!

0 人點贊