詳解Java執(zhí)行groovy腳本的兩種方式
記錄Java執(zhí)行groovy腳本的兩種方式,簡單粗暴:
一種是通過腳本引擎ScriptEngine提供的eval(String)方法執(zhí)行腳本內(nèi)容;一種是執(zhí)行groovy腳本;
二者都通過Invocable來傳遞參數(shù)并獲取執(zhí)行結果;
Invocable:腳本引擎的解釋器接口,提供invokeFunction和invokeMethod兩種傳遞參數(shù)并獲取執(zhí)行結果的方法,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í)行結果
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方法的第一個參數(shù)為腳本的函數(shù)名稱,把scriptName拎出來通過創(chuàng)建String對象再賦值進去,方便你看懂函數(shù)名稱到底是哪個;scriptContent中${date.getTime()}與$name的意思一樣,grovvy中的字符串可以識別${}和$占位符;bindings綁定參數(shù)與invokeFunction方法的第二個參數(shù)的區(qū)別是,前者是腳本內(nèi)全局的,而后者是定義在函數(shù)內(nèi)的;
例如把腳本內(nèi)容定義為這樣:

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

實例化腳本對象并執(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í)行腳本并獲取結果
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;
}
}
運行結果:

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í)行結果
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í)行腳本并獲取結果
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執(zhí)行groovy腳本內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
淺談Java中Lock和Synchronized的區(qū)別
這篇文章主要介紹了Java中Lock和Synchronized的區(qū)別,Lock和Synchronized都是java中去用來解決線程安全問題的一個工具,但是具體有什么區(qū)別呢?下面我們一起進入文章了解具體詳細內(nèi)容吧,需要的朋友可以參考一下2022-04-04
SpringMVC 接收前端傳遞的參數(shù)四種方式小結
這篇文章主要介紹了SpringMVC 接收前端傳遞的參數(shù)四種方式小結,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10
Springboot之@Async不執(zhí)行原因及分析
這篇文章主要介紹了Springboot之@Async不執(zhí)行原因及分析,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-09-09
日志模塊自定義@SkipLogAspect注解跳過切面的操作方法
文章介紹了一個自定義注解@SkipLogAspect,用于在日志模塊中跳過特定方法的日志切面,這個注解可以用于需要避免大對象轉換為JSON時導致的OOM問題,文章還提供了注解的實現(xiàn)代碼以及一個測試示例,展示了如何在控制器中使用該注解來跳過日志切面,感興趣的朋友一起看看吧2025-02-02

