詳解Java執(zhí)行g(shù)roovy腳本的兩種方式
記錄Java執(zhí)行g(shù)roovy腳本的兩種方式,簡(jiǎn)單粗暴:
一種是通過(guò)腳本引擎ScriptEngine提供的eval(String)方法執(zhí)行腳本內(nèi)容;一種是執(zhí)行g(shù)roovy腳本;
二者都通過(guò)Invocable來(lái)傳遞參數(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(); }}
運(yùn)行結(jié)果:
例如把腳本內(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.groovyclass 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(){// 初始化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腳本對(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)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. JavaWeb Servlet中url-pattern的使用2. 淺談SpringMVC jsp前臺(tái)獲取參數(shù)的方式 EL表達(dá)式3. asp(vbscript)中自定義函數(shù)的默認(rèn)參數(shù)實(shí)現(xiàn)代碼4. React優(yōu)雅的封裝SvgIcon組件示例5. 輕松學(xué)習(xí)XML教程6. php網(wǎng)絡(luò)安全中命令執(zhí)行漏洞的產(chǎn)生及本質(zhì)探究7. ASP刪除img標(biāo)簽的style屬性只保留src的正則函數(shù)8. jsp中sitemesh修改tagRule技術(shù)分享9. ASP基礎(chǔ)知識(shí)VBScript基本元素講解10. 詳解瀏覽器的緩存機(jī)制
