亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

詳解Java執(zhí)行g(shù)roovy腳本的兩種方式

 更新時(shí)間:2021年04月21日 08:21:00   作者:蒲公英不是夢(mèng)  
這篇文章主要介紹了Java執(zhí)行g(shù)roovy腳本的兩種方式,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

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

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

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

Invocable:腳本引擎的解釋器接口,提供invokeFunctioninvokeMethod兩種傳遞參數(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();
    }
}

運(yùn)行結(jié)果:

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

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

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

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

public void testByMethod(){
    try {
        // 初始化groovy腳本對(duì)象
        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;
    }
}

運(yùn)行結(jié)果:

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

源碼:

package com.dandelion.test;

import com.dandelion.groovy.TestGroovy;

import javax.script.*;
import java.util.Date;

/**
 * ================================
 * 測(cè)試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腳本對(duì)象
            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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java之HashMap.values()方法的誤用解讀

    Java之HashMap.values()方法的誤用解讀

    這篇文章主要介紹了Java之HashMap.values()方法的誤用解讀,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • Spring Boot 集成 Kafkad的實(shí)現(xiàn)示例

    Spring Boot 集成 Kafkad的實(shí)現(xiàn)示例

    這篇文章主要介紹了Spring Boot 集成 Kafkad的示例,幫助大家更好的理解和學(xué)習(xí)使用Spring Boot框架,感興趣的朋友可以了解下
    2021-04-04
  • 詳解log4j.properties的簡(jiǎn)單配置和使用

    詳解log4j.properties的簡(jiǎn)單配置和使用

    本篇文章主要介紹了詳解log4j.properties的簡(jiǎn)單配置和使用,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-12-12
  • springmvc fastjson 反序列化時(shí)間格式化方法(推薦)

    springmvc fastjson 反序列化時(shí)間格式化方法(推薦)

    下面小編就為大家?guī)硪黄猻pringmvc fastjson 反序列化時(shí)間格式化方法(推薦)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-04-04
  • 詳解JUC并發(fā)編程之鎖

    詳解JUC并發(fā)編程之鎖

    這篇文章主要為大家介紹了JUC并發(fā)編程之鎖,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2021-12-12
  • JavaSE文件操作工具類FileUtil詳解

    JavaSE文件操作工具類FileUtil詳解

    這篇文章主要為大家詳細(xì)介紹了JavaSE系列之文件操作工具類FileUtil,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-08-08
  • Java反射機(jī)制詳解

    Java反射機(jī)制詳解

    這篇文章主要介紹了Java反射機(jī)制,首先簡(jiǎn)單介紹了反射機(jī)制的預(yù)備知識(shí),進(jìn)一步分析了Java反射機(jī)制的原理、實(shí)現(xiàn)技巧與應(yīng)用方法,需要的朋友可以參考下
    2015-12-12
  • 向Spring IOC 容器動(dòng)態(tài)注冊(cè)bean實(shí)現(xiàn)方式

    向Spring IOC 容器動(dòng)態(tài)注冊(cè)bean實(shí)現(xiàn)方式

    這篇文章主要為大家介紹了向Spring IOC 容器動(dòng)態(tài)注冊(cè)bean實(shí)現(xiàn)方式詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-07-07
  • SpringBoot整合Lucene實(shí)現(xiàn)全文檢索的詳細(xì)步驟

    SpringBoot整合Lucene實(shí)現(xiàn)全文檢索的詳細(xì)步驟

    全文搜索(Full-Text?Search)是指對(duì)大規(guī)模存儲(chǔ)在計(jì)算機(jī)系統(tǒng)中的文本數(shù)據(jù)進(jìn)行檢索和匹配的技術(shù),它允許用戶輸入關(guān)鍵字,然后從海量的文本數(shù)據(jù)中快速找到相關(guān)的信息,本文介紹了SpringBoot整合Lucene實(shí)現(xiàn)全文檢索的詳細(xì)步驟,需要的朋友可以參考下
    2024-03-03
  • Maven高級(jí)的聚合和繼承的實(shí)現(xiàn)

    Maven高級(jí)的聚合和繼承的實(shí)現(xiàn)

    在軟件開發(fā)中,隨著項(xiàng)目規(guī)模的擴(kuò)大,單個(gè)模塊的開發(fā)方式逐漸轉(zhuǎn)變?yōu)槎嗄K開發(fā),這種方式帶來了項(xiàng)目管理上的挑戰(zhàn),其中最常見的問題是模塊間的依賴管理和版本控制問題,本文就來介紹一下
    2024-10-10

最新評(píng)論