Springboot中动态语言groovy介绍

目录
  • Groovy
    • pom
    • ResourceScriptSource
    • DatabaseScriptSource

Groovy

Groovy是一种基于Java的语法的基于JVM的编程语言。Groovy支持动态输入,闭包,元编程,运算符重载等等语法。除此之外,Groovy还提供了许多类似脚本语言的功能,比如,多行字符串,字符串插值,优雅的循环结构和简单的属性访问。另外,结尾分号是可选的。而这些都有足够的理帮助开发人员为了提高开发效率。

换句话说,Groovy就是一种继承了动态语言的优良特性并运行在JVM上的编程语言。由于Groovy的语法非常接近Java,所以Java开发人员很容易开始使用Groovy。 Spring Boot应用中也支持使用Groovy编程语言进行开发。

  • ResourceScriptSource:在 resources 下面写groovy类
  • StaticScriptSource:把groovy类代码放进XML里
  • DatabaseScriptSource:把groovy类代码放进数据库中

pom

<!-- groovy -->
<dependency>
    <artifactId>groovy</artifactId>
    <groupId>org.codehaus.groovy</groupId>
    <version>2.5.8</version>
    <scope>compile</scope>
</dependency>

ResourceScriptSource

@SpringBootApplication
public class DemoApplication {
    public static void main(String[] args) {
        ConfigurableApplicationContext run = SpringApplication.run(DemoApplication.class, args);
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-groovy.xml");
        GroovyService bean = context.getBean(GroovyService.class);
        String sayHello = bean.sayHello();
        System.out.println(sayHello);
    }
}
public interface GroovyService {
    String sayHello();
}

spring-groovy.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:lang="http://www.springframework.org/schema/lang"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/lang
                           http://www.springframework.org/schema/lang/spring-lang.xsd">
    <lang:groovy id="helloService">
        <lang:inline-script>
            import com.example.demo.groovy.GroovyService
            class HelloServiceImpl implements GroovyService {
                String name;
                @Override
                String sayHello() {
                    return "Hello $name. Welcome to static script in Groovy.";
                }
            }
        </lang:inline-script>
        <lang:property name="name" value="maple"/>
    </lang:groovy>
</beans>

DatabaseScriptSource

方法一:

实时读取DB里的groovy脚本文件

利用GroovyClassLoader去编译脚本文件

把class对象注入成Spring bean

反射调用脚本的方法

CREATE TABLE `groovy_script` (
  `id` bigint NOT NULL AUTO_INCREMENT,
  `script_name` varchar(64) NOT NULL COMMENT 'script name',
  `script_content` text NOT NULL COMMENT 'script content',
  `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=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='groovy script';
INSERT INTO book_shop2.groovy_script
(id, script_name, script_content, status, extend_info, created_time, modified_time)
VALUES(1, 'groovyService', 'import com.example.demo.groovy.GroovyService
            class HelloServiceImpl implements GroovyService {
				@Override
				String sayHello() {
					return "sayHello";
				}
                @Override
                String sayHello(String name) {
                    return "Hello " + name + ". Welcome to static script in Groovy.";
                }
            }', 'ENABLE', NULL, '2020-09-26 17:16:36.477818000', '2022-09-04 22:54:51.421959000');
@RestController
public class GroovyController {
    @Autowired
    GroovyScriptMapper groovyScriptMapper;
    @GetMapping("/aaaa")
    private String groovyTest() throws IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchMethodException {
        GroovyScript groovyScript = this.groovyScriptMapper.getOne(1L);
        System.out.println(groovyScript.getScriptContent());
        Class clazz = new GroovyClassLoader().parseClass(groovyScript.getScriptContent());
        Object o = clazz.newInstance();
        SpringContextUtils.autowireBean(o);
        Method method = clazz.getMethod("sayHello", String.class);
        String aaaaaaa = (String) method.invoke(o, "aaaaaaa");
        System.out.println(aaaaaaa);
        return aaaaaaa;
    }
}
/*
import com.example.demo.groovy.GroovyService
            class HelloServiceImpl implements GroovyService {
				@Override
				String sayHello() {
					return "sayHello";
				}
                @Override
                String sayHello(String name) {
                    return "Hello " + name + ". Welcome to static script in Groovy.";
                }
            }
Hello aaaaaaa. Welcome to static script in Groovy.
*/
public interface GroovyScriptMapper extends BaseMapper<GroovyScript> {
    @Select({"select script_content from groovy_script where id = #{id}"})
    @Result(column = "script_content", property = "scriptContent")
    GroovyScript getOne(Long id);
}
@Component
public class SpringContextUtils implements ApplicationContextAware {
    static ApplicationContext context;
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringContextUtils.context = applicationContext;
    }
    public static void autowireBean(Object bean) {
        context.getAutowireCapableBeanFactory().autowireBean(bean);
    }
    public static ApplicationContext getContext() {
        return context;
    }
    public static <T> T getBean(Class<T> clazz) {
        return context.getBean(clazz);
    }
    public static <T> T getBean(String name) {
        return (T) context.getBean(name);
    }
}

到此这篇关于Springboot中动态语言groovy介绍的文章就介绍到这了,更多相关Springboot groovy内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • SpringBoot项目中使用Groovy脚本的示例代码

    目录 1. 引入依赖 2. 使用脚本引擎运行groovy脚本 3.思考 SpringBoot+Groovy运行动态脚本 GroovyClassLoader方式 GroovyScriptEngine方式 变量绑定 最近项目中遇到了这样的需求:需要检查一个表的某些字段,是否为空,或者是否符合预期规则:比如大于0,或者在某个范围内.考虑将表名和字段名配置在数据库中,然后规则使用Groovy来写,比较灵活. 1. 引入依赖 <dependency> <groupId>org.codehau

  • SpringBoot整合Groovy脚本实现动态编程详解

    目录 Groovy简介 应用场景 集成与使用 第一步.与SpringBoot集成 1.pom.xml文件如下: 第二步.写出Groovy版本的“Hello World” 1.HelloWorld.groovy脚本代码 2.创建测试类GroovyTest.java 3.运行结果 第三步.传入变量与获取返回值 1.变量与返回值Groovy脚本代码 2.创建测试类GroovyTest2.java 3.运行结果 第四步.启动SpringBoot 1.创建SpringContextUtil.java 2.

  • Springboot中动态语言groovy介绍

    目录 Groovy pom ResourceScriptSource DatabaseScriptSource Groovy Groovy是一种基于Java的语法的基于JVM的编程语言.Groovy支持动态输入,闭包,元编程,运算符重载等等语法.除此之外,Groovy还提供了许多类似脚本语言的功能,比如,多行字符串,字符串插值,优雅的循环结构和简单的属性访问.另外,结尾分号是可选的.而这些都有足够的理帮助开发人员为了提高开发效率. 换句话说,Groovy就是一种继承了动态语言的优良特性并运行在J

  • 详细聊聊SpringBoot中动态切换数据源的方法

    其实这个表示有点不太对,应该是 Druid 动态切换数据源的方法,只是应用在了 springboot 框架中,准备代码准备了半天,之前在一次数据库迁移中使用了,发现 Druid 还是很强大的,用来做动态数据源切换很方便. 首先这里的场景跟我原来用的有点点区别,在项目中使用的是通过配置中心控制数据源切换,统一切换,而这里的例子多加了个可以根据接口注解配置 第一部分是最核心的,如何基于 Spring JDBC 和 Druid 来实现数据源切换,是继承了org.springframework.jdbc

  • Swift中动态调用实例方法介绍

    在 Swift 中有一类很有意思的写法,可以让我们不直接使用实例来调用这个实例上的方法,而是通过类型取出这个类型的某个实例方法的签名,然后再通过传递实例来拿到实际需要调用的方法.比如我们有这样的定义: 复制代码 代码如下: class MyClass {     func method(number: Int) -> Int {         return number + 1     } } 想要调用 method 方法的话,最普通的使用方式是生成MyClass的实例,然后用.method来

  • springboot中使用groovy的示例代码

    目录 Groovy pom ResourceScriptSource DatabaseScriptSource Groovy Groovy是一种基于Java的语法的基于JVM的编程语言.Groovy支持动态输入,闭包,元编程,运算符重载等等语法.除此之外,Groovy还提供了许多类似脚本语言的功能,比如,多行字符串,字符串插值,优雅的循环结构和简单的属性访问.另外,结尾分号是可选的.而这些都有足够的理帮助开发人员为了提高开发效率. 换句话说,Groovy就是一种继承了动态语言的优良特性并运行在J

  • springboot中mybatis多数据源动态切换实现

    目录 多数据源配置引入 动态数据源路由实现 动态数据源切换使用 案例源码 在开发中,动态数据源配置还是用的比较多的,比如在多数据源使用方面,又或者是在多个DB之间切换方面.这里给出一个动态数据源的配置方案,两个DB均以mysql为例. 多数据源配置引入 mybatis和mysql在springboot中的引入这里就不在说了,不了解的可以参见springboot中mysql与mybatis的引入. 数据源配置如下: datasource: master: type: com.alibaba.dru

  • 关于C语言动态内存管理介绍

    目录 1.为什么需要动态内存分配 2.有关动态内存函数介绍 2.1 malloc和free 2.2 calloc函数 2.3 realloc函数 3. 常见的动态内存错误 3.1 对NULL指针进行解引用操作 3.2 对动态开辟空间的越界访问 3.3 对非动态开辟内存使用free释放 3.4 使用free释放一块动态开辟内存的一部分 3.5 对同一块动态内存多次释放 3.6 动态开辟内存忘记释放(内存泄漏) 总结 1.为什么需要动态内存分配 关于这个问题,我们先看看我们之前是如何开辟内存的. i

  • 详细谈谈C语言中动态内存

    目录 前言 1.关于动态内存的函数 1.1malloc和free函数 1.2calloc函数 1.3realloc函数 2.常见的动态内存错误 2.1对NULL指针解引用 2.2对动态内存开辟的空间越界访问 2.3 对非动态开辟内存使用free释放 2.4 使用free释放一块动态开辟内存的一部分 2.5对同一块动态内存多次释放 2.6内存泄漏 补充:为什么要引入动态内存分配 总结 前言 关于动态内存管理,可能有学习过的小伙伴,也有没有听说过的.没有听说过的小伙伴会觉得很奇怪啊,为什么要动态开辟

  • C语言中动态内存分配malloc、calloc和realloc函数解析

    目录 前言 free函数 malloc函数 calloc函数 realloc函数 扩充 malloc/calloc/realloc区别总结 总结 前言 有时候我们需要的空间大小不确定,需要随着程序需要的空间而变化, 那以数组开辟的固定大小的空间就不适用了, 这时候我们就需要动态分配开辟空间了.当空间不够时就扩容.动态开辟是在堆区开辟一块连续可用空间,并返回这块空间的地址.有三种函数malloc, calloc和realloc.我们动态内存分配就在堆区开辟空间 上面的四个区只有堆区的空间是需要手动

  • C语言中动态内存管理图文详解

    目录 1.动态内存开辟的原因 2.动态内存函数的介绍 2.1malloc和free 2.2calloc 2.3realloc 3.常见的动态内存错误 3.1对NULL指针的解引用操作 3.2对动态开辟空间的越界访问 3.3对非动态开辟内存使用free 3.4使用释放一块动态开辟内存的一部分 3.5对同一块动态内存多次释放 3.6动态开辟内存忘记释放(内存泄漏) 4.练习 4.1练习1 4.1练习2 4.3练习3 4.4练习4 5.C/C++程序的内存开辟 总结 1.动态内存开辟的原因 常见的内存

  • Groovy动态语言使用教程简介

    目录 Groovy 简介 Groovy 应用 Groovy 与 Java Groovy语法特性(相比于Java) Groovy 简介 Groovy 是构建在 JVM 上的一个轻量级却强大的动态语言,它结合了 Python.Ruby 和 Smalltalk 的许多强大的特性. Groovy 就是用 Java 写的,Groovy 语法与 Java 语法类似,Groovy 代码能够与 Java 代码很好地结合,也能用于扩展现有代码.相对于 Java,它在编写代码的灵活性上有非常明显的提升,Groovy

随机推荐