springboot集成groovy动态执行代码
springboot版本号:2.2.2.RELEASE
在pom.xml中集成groovy
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>1.8.9</version>
<scope>compile</scope>
</dependency>
一.springboot中执行groovy文件
main文件直接调用groovy文件,有三种方式
方式一生成Test.groovy:
package com.example.demo.groovy
public class Test {
public static String test(String id) {
return "younger--->" + id;
}
}
方式二生成test1.groovy
package com.example.demo.groovy.groovyscript
def test(id){
return "test2 id:"+ id;
}
方法三生成test2.groovy
package com.example.demo.groovy.groovyscript
output = "test3 id: ${id}, name: ${name}"
通过Java的main方法调用groovy文件,得到执行结束
package com.example.demo;
import com.example.demo.controller.CalculateController;
import groovy.lang.Binding;
import groovy.lang.GroovyClassLoader;
import groovy.lang.GroovyObject;
import groovy.lang.Script;
import groovy.util.GroovyScriptEngine;
import groovy.util.ResourceException;
import groovy.util.ScriptException;
import java.io.File;
import java.io.IOException;
/**
* @program: demo
* @description: 本地main方法执行groovy文件
* @author: younger
* @create: 2021-05-07 15:42
**/
public class GroovyTest {
public static void main(String[] args) throws IOException, IllegalAccessException, InstantiationException, ResourceException, ScriptException {
//方式一调用groovy文件
ClassLoader parent = CalculateController.class.getClassLoader();
GroovyClassLoader loader = new GroovyClassLoader(parent);
Class groovyClass = loader.parseClass(new File("src/main/java/com/example/demo/groovy/Test.groovy"));
//得到groovy对象
GroovyObject groovyObject= (GroovyObject)groovyClass.newInstance();
//执行对象的test方法,并传参数-"id---1"
String result = (String) groovyObject.invokeMethod("test", "id---1");
System.out.println("result--------->" + result);
//方式二调用groovy文件,找到groovy脚本所在文件夹
GroovyScriptEngine engine = new GroovyScriptEngine("src/main/java/com/example/demo/groovy/groovyscript");
//得到Script对象
Script script = engine.createScript("test1.groovy", new Binding());
//执行Script对象的test方法,并传参数-"id---1"
result = (String) script.invokeMethod("test", "id----2");
System.out.println("result--------->" + result);
//方式三调用groovy文件
// GroovyScriptEngine engine = new GroovyScriptEngine("src/main/java/com/example/demo/groovy/groovyscript");
Binding binding = new Binding();
//封装参数
binding.setVariable("id","id---3");
binding.setVariable("name", "younger");
//执行test2.groovy脚本
engine.run("test2.groovy", binding);
//返回output
result = binding.getVariable("output").toString();
System.out.println("result--------->" + result);
}
}
二,通过数据库保存groovy内容,动态执行groovy脚本
数据库mysql,创建groovy规则表:calculate_rule
CREATE TABLE `calculate_rule` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`interface_id` varchar(128) NOT NULL COMMENT '接口id',
`bean_name` varchar(64) NOT NULL COMMENT 'bean_name',
`calculate_rule` text NOT NULL COMMENT 'groovy脚本内容',
`calculate_type` varchar(64) NOT NULL COMMENT '状态',
`status` varchar(16) NOT NULL DEFAULT 'ENABLE' COMMENT 'ENABLE-启用/DISENABLE-停用',
`extend_info` varchar(4096) DEFAULT NULL,
`created_time` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
`modified_time` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6),
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COMMENT='calculate rule';
insert into `calculate_rule` (`id`, `interface_id`, `bean_name`, `calculate_rule`, `calculate_type`, `status`, `extend_info`, `cr