国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術(shù)文章
文章詳情頁

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

瀏覽:59日期:2022-08-14 09:51:58

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

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

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

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

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

invokeFunction:

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

invokeMethod:

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

以下為案例:

引入依賴

<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é)果:

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

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

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

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

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

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

實例化腳本對象并執(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.groovyclass TestGroovy { static def execute(Date date, String name){println('now dateTime is: ${date.getTime()}');println('my name is $name');return date.getTime() < 0; }}

運行結(jié)果:

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

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(){// 初始化BindingsBindings 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)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Java
相關(guān)文章:
主站蜘蛛池模板: 绵阳市| 分宜县| 马公市| 河源市| 四子王旗| 阳谷县| 红原县| 三都| 运城市| 额敏县| 繁昌县| 石台县| 巴青县| 新乡县| 杭锦后旗| 丹江口市| 东乡族自治县| 和龙市| 延安市| 沈阳市| 洪湖市| 雷波县| 米脂县| 中江县| 新野县| 卓资县| 如皋市| 舟山市| 四会市| 武夷山市| 嘉禾县| 贵德县| 虞城县| 堆龙德庆县| 常山县| 吉安县| 贡觉县| 长阳| 阳高县| 饶平县| 缙云县|