最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

一招教你使用Java執(zhí)行g(shù)roovy腳本的兩種方式

 更新時間:2023年09月26日 15:19:53   作者:煙火纏過客  
本文主要介紹了一招教你使用Java執(zhí)行g(shù)roovy腳本的兩種方式,一種是通過腳本引擎ScriptEngine提供的eval(String)方法執(zhí)行腳本內(nèi)容,一種是執(zhí)行g(shù)roovy腳本,感興趣的可以了解一下

介紹

記錄 Java 執(zhí)行 groovy 腳本的兩種

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

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

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

invokeFunction:

invokeMethod:

以下為案例:

引入依賴

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

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

public void testByFunction(){
    // 初始化Bindings
    Bindings bindings = engine.createBindings();
    // 綁定參數(shù)
    bindings.put("date", new Date());
    final String name = "groovy";
    // 定義groovy腳本中執(zhí)行方法的名稱
    final String scriptName = "execute";
    // 定義groovy腳本內(nèi)容
    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í)行結(jié)果
        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();
    }
}

運行結(jié)果:

  • invokeFunction 方法的第一個參數(shù)為腳本的函數(shù)名稱,把 scriptName 拎出來通過創(chuàng)建 String 對象再賦值進(jìn)去,方便你看懂函數(shù)名稱到底是哪個;
  • scriptContent ${date.getTime()} $name 的意思一樣, grovvy 中的字符串可以識別 ${} $ 占位符;
  • bindings 綁定參數(shù)與 invokeFunction 方法的第二個參數(shù)的區(qū)別是,前者是腳本內(nèi)全局的,而后者是定義在函數(shù)內(nèi)的;

例如把腳本內(nèi)容定義為這樣:

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

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

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

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

運行結(jié)果:

invokeMethod 方法的第一個參數(shù)是腳本對象,第二個參數(shù)是腳本中的函數(shù)名稱,之后為綁定的參數(shù);

源碼:

package com.dandelion.test;
import com.dandelion.groovy.TestGroovy;
import javax.script.*;
import java.util.Date;
/**
 * ================================
 * 測試groovy腳本的執(zhí)行方式
 * @Author Him
 * @Date 2021-04-21
 * @Time 01:12
 * ================================
 */
public class TestScriptEngine {
    // 查找并創(chuàng)建指定腳本引擎
    private ScriptEngine engine = new ScriptEngineManager().getEngineByName("groovy");
    public void testByFunction(){
        // 初始化Bindings
        Bindings bindings = engine.createBindings();
        // 綁定參數(shù)
        bindings.put("date", new Date());
        // 定義groovy腳本中執(zhí)行方法的名稱
        final String scriptName = "execute";
        // 定義groovy腳本內(nèi)容
        final String scriptContent = "def " + scriptName +"(){" +
                                     "    println(\"now dateTime is: ${date.getTime()}\");" +
                                     "    return date.getTime() > 0;" +
                                     "}";
        try {
            // 執(zhí)行腳本
            engine.eval(scriptContent, bindings);
            // 獲取執(zhí)行結(jié)果
            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";
            // 定義參數(shù)
            final Date arg_1 = new Date();
            final String arg_2 = "groovy";
            // 執(zhí)行腳本并獲取結(jié)果
            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();
    }
}

到此這篇關(guān)于一招教你使用Java執(zhí)行g(shù)roovy腳本的兩種方式的文章就介紹到這了,更多相關(guān)Java執(zhí)行g(shù)roovy腳本內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家! 

相關(guān)文章

最新評論

辽宁省| 扬中市| 米泉市| 姚安县| 洪泽县| 江城| 景宁| 玉树县| 文水县| 甘德县| 泸西县| 札达县| 翼城县| 格尔木市| 永胜县| 东源县| 睢宁县| 高雄市| 兴文县| 宁乡县| 天津市| 沈丘县| 清丰县| 庆阳市| 武隆县| 鄢陵县| 安多县| 六安市| 浑源县| 来安县| 邢台县| 太和县| 沂南县| 介休市| 分宜县| 衡山县| 台江县| 张北县| 宣化县| 清徐县| 洪洞县|