springboot集成groovy执行代码

本文介绍了如何在SpringBoot 2.2.2.RELEASE项目中集成Groovy并实现动态执行代码。首先,文章讲解了在主文件中通过三种方式直接调用Groovy文件执行。接着,详细阐述了通过数据库保存Groovy脚本内容,利用MySQL创建groovy规则表,并在启动时动态加载到Spring容器中执行的步骤,包括GroovyDynamicConfiguration类的创建和Controller的测试方法。最后提供了代码下载链接。
摘要由CSDN通过智能技术生成

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值