引入依赖
定义脚本内容并执行
public void testByFunction(){
// 初始化Bindings
Bindings bindings = engine.createBindings();
// 绑定参数
bindings.put("date", new Date());
final String name = "groovy";
// 定义groovy脚本中执行方法的名称
final String scriptName = "execute";
// 定义groovy脚本内容
final String scriptContent = "def " + scriptName +"(name){" +
" println(\"now dateTime is: ${date.getTime()}\");" +
" println(\"my name is $name\");" +
" return date.getTime() > 0;" +
"}";
try {
// 执行脚本
engine.eval(scriptContent, bindings);
// 获取执行结果
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方法的第一个参数为脚本的函数名称,把scriptName拎出来通过创建String对象再赋值进去,方便你看懂函数名称到底是哪个;
scriptContent中${date.getTime()}与$name的意思一样,grovvy中的字符串可以识别${}和$占位符;
bindings绑定参数与invokeFunction方法的第二个参数的区别是,前者是脚本内全局的,而后者是定义在函数内的;
例如把脚本内容定义为这样:
执行结果就是这样了:
实例化脚本对象并执行
public void testByMethod(){
try {
// 初始化groovy脚本对象
final TestGroovy testGroovy = new TestGroovy();
// 定义groovy脚本中执行方法的名称
final String scriptName = "execute";
// 定义参数
final Date arg_1 = new Date();
final String arg_2 = "groovy";
// 执行脚本并获取结果
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脚本内容:
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方法的第一个参数是脚本对象,第二个参数是脚本中的函数名称,之后为绑定的参数;
源码:
package com.dandelion.test;
import com.dandelion.groovy.TestGroovy;
import javax.script.*;
import java.util.Date;
/**
* ================================
* 测试groovy脚本的执行方式
* @Author Him
* @Date 2021-04-21
* @Time 01:12
* ================================
*/
public class TestScriptEngine {
// 查找并创建指定脚本引擎
private ScriptEngine engine = new ScriptEngineManager().getEngineByName("groovy");
public void testByFunction(){
// 初始化Bindings
Bindings bindings = engine.createBindings();
// 绑定参数
bindings.put("date", new Date());
// 定义groovy脚本中执行方法的名称
final String scriptName = "execute";
// 定义groovy脚本内容
final String scriptContent = "def " + scriptName +"(){" +
" println(\"now dateTime is: ${date.getTime()}\");" +
" return date.getTime() > 0;" +
"}";
try {
// 执行脚本
engine.eval(scriptContent, bindings);
// 获取执行结果
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脚本中执行方法的名称
final String scriptName = "execute";
// 定义参数
final Date arg_1 = new Date();
final String arg_2 = "groovy";
// 执行脚本并获取结果
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();
}
}]
USB Microphone https://www.soft-voice.com/
Wooden Speakers https://www.zeshuiplatform.com/
亚马逊测评 www.yisuping.cn
深圳网站建设www.sz886.com