|
| 1 | +# tio-boot 整合 Groovy |
| 2 | + |
| 3 | +## 什么是 Groovy |
| 4 | + |
| 5 | +Groovy 是一种运行在 Java 平台上的动态语言,它完全与 Java 兼容,同时也提供了一些更现代化的编程特性。Groovy 是为了提高开发人员的生产力而设计的,它的语法对于 Java 开发者来说很容易上手,因为 Groovy 可以无缝地与 Java 代码和库集成。 |
| 6 | + |
| 7 | +### Groovy 的主要特点包括: |
| 8 | + |
| 9 | +1. **简化的语法**:Groovy 的语法比 Java 更简洁,去掉了一些冗余的关键字和标点,例如分号可以省略,方法定义不需要指定返回类型(如果不想指定的话)。 |
| 10 | +2. **动态特性**:Groovy 是一种动态语言,它支持运行时元编程、动态类型、闭包等特性。这使得 Groovy 非常适合编写脚本或快速开发应用。 |
| 11 | +3. **强大的集合处理能力**:Groovy 提供了扩展的集合 API 和一系列内建的集合处理方法,如`each`, `collect`, `find`, `groupBy`等,这些方法大大简化了对集合的操作。 |
| 12 | +4. **内建语言特性**:Groovy 内置了对 XML 和 JSON 的直接支持,使得处理这些数据格式非常便捷。 |
| 13 | +5. **闭包支持**:Groovy 的闭包是对 Java 匿名内部类的一种强大的替代方式,它支持简短语法和直接访问闭包外部的变量。 |
| 14 | +6. **编译和脚本双模式**:Groovy 代码可以被编译成 Java 字节码,也可以作为脚本直接运行,这为开发提供了极大的灵活性。 |
| 15 | +7. **与 Java 的无缝集成**:Groovy 可以直接使用所有的 Java 类库,Java 代码也可以直接调用 Groovy 类和方法,这使得 Groovy 在现有的 Java 项目中非常容易被集成。 |
| 16 | + |
| 17 | +### 应用场景 |
| 18 | + |
| 19 | +1. **快速开发**:Groovy 的动态特性和简化的语法使其成为快速开发原型和小型应用的理想选择。 |
| 20 | +2. **测试脚本**:Groovy 常被用于编写自动化测试脚本,特别是在使用 Spock(一个基于 Groovy 的测试框架)时。 |
| 21 | +3. **构建脚本**:Groovy 是 Gradle 构建工具的基础语言,这使得 Groovy 成为编写构建和部署脚本的流行选择。 |
| 22 | +4. **Web 应用开发**:Groovy 可以用于 Web 应用开发,尤其是与 Grails 框架结合使用时。Grails 是一个基于 Groovy 的全栈 Web 应用框架,提供了类似于 Ruby on Rails 的开发体验。 |
| 23 | + |
| 24 | +Groovy 的这些特性使它成为 Java 生态系统中一个非常有价值和灵活的补充,能够在许多不同的开发场景中提供帮助。 |
| 25 | + |
| 26 | +## tio-boot 整合 Groovy 脚本 |
| 27 | + |
| 28 | +### 在 tio-boot 中调用 Groovy 脚本处理 web 请求 |
| 29 | + |
| 30 | +要在 tio-boot 中调用并运行 Groovy 脚本,你首先需要添加 Groovy 语言的支持,然后编写相应的 Java 代码来执行 Groovy 脚本。下面是详细的步骤和示例: |
| 31 | + |
| 32 | +#### 1. 添加 Groovy 依赖 |
| 33 | + |
| 34 | +在你的项目中,需要添加 Groovy 的依赖来支持 Groovy 脚本的执行。在你的 `pom.xml` 文件中添加以下依赖: |
| 35 | + |
| 36 | +```xml |
| 37 | +<dependency> |
| 38 | + <groupId>org.codehaus.groovy</groupId> |
| 39 | + <artifactId>groovy</artifactId> |
| 40 | + <version>3.0.9</version> <!-- 请根据实际情况选择适合的版本 --> |
| 41 | +</dependency> |
| 42 | +``` |
| 43 | + |
| 44 | +#### 2. 编写 Groovy 脚本处理器 |
| 45 | + |
| 46 | +创建一个用于执行 Groovy 脚本的处理器。这里我们模仿你之前提供的 Magic Script 的用法来整合 Groovy 脚本。 |
| 47 | + |
| 48 | +```java |
| 49 | +package com.litongjava.grovvy; |
| 50 | + |
| 51 | +import java.io.BufferedReader; |
| 52 | +import java.io.InputStream; |
| 53 | +import java.io.InputStreamReader; |
| 54 | +import java.nio.charset.StandardCharsets; |
| 55 | +import java.util.stream.Collectors; |
| 56 | + |
| 57 | +import com.litongjava.jfinal.aop.Aop; |
| 58 | +import com.litongjava.tio.utils.hutool.ResourceUtil; |
| 59 | + |
| 60 | +import groovy.lang.GroovyShell; |
| 61 | + |
| 62 | +public class GroovyScriptManager { |
| 63 | + |
| 64 | + public static Object executeScript(String script) { |
| 65 | + GroovyShell shell = Aop.get(GroovyShell.class); |
| 66 | + return shell.evaluate(script); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Execute a script located in the classpath. |
| 71 | + */ |
| 72 | + @SuppressWarnings("unchecked") |
| 73 | + public static <T> T executeClasspathScript(String filename) { |
| 74 | + try (InputStream inputStream = ResourceUtil.getResourceAsStream(filename)) { |
| 75 | + if (inputStream != null) { |
| 76 | + try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8))) { |
| 77 | + String script = reader.lines().collect(Collectors.joining("\n")); |
| 78 | + return (T) executeScript(script); |
| 79 | + } |
| 80 | + } else { |
| 81 | + throw new IllegalArgumentException("Script file not found: " + filename); |
| 82 | + } |
| 83 | + } catch (Exception e) { |
| 84 | + throw new RuntimeException("Error executing Groovy script", e); |
| 85 | + } |
| 86 | + } |
| 87 | +} |
| 88 | +``` |
| 89 | + |
| 90 | +#### 3. 创建 Groovy 脚本 |
| 91 | + |
| 92 | +在 `src/main/resources/groovy` 下创建一个名为 `web_hello.groovy` 的脚本文件,内容如下: |
| 93 | + |
| 94 | +```groovy |
| 95 | +// web_hello.groovy |
| 96 | +import com.litongjava.tio.http.common.HttpRequest |
| 97 | +import com.litongjava.tio.http.common.HttpResponse |
| 98 | +import com.litongjava.tio.boot.http.TioControllerContext |
| 99 | +import com.litongjava.tio.utils.resp.RespVo |
| 100 | +
|
| 101 | +// 获取请求和响应对象 |
| 102 | +def request = TioControllerContext.getRequest() |
| 103 | +def response = TioControllerContext.getResponse() |
| 104 | +
|
| 105 | +// 执行数据库查询 |
| 106 | +def sql = "select * from test_data where name=?" |
| 107 | +println(sql) |
| 108 | +
|
| 109 | +// 设置响应对象 |
| 110 | +response.setJson(RespVo.ok(sql)) |
| 111 | +return response |
| 112 | +``` |
| 113 | + |
| 114 | +#### 4. 添加 HTTP 请求处理配置 |
| 115 | + |
| 116 | +添加`HttpServerRequestHandlerConfig` 类来调用 Groovy 脚本: |
| 117 | + |
| 118 | +```java |
| 119 | +package com.litongjava.tio.web.hello.config; |
| 120 | + |
| 121 | +import com.litongjava.jfinal.aop.annotation.AInitialization; |
| 122 | +import com.litongjava.jfinal.aop.annotation.BeforeStartConfiguration; |
| 123 | +import com.litongjava.tio.web.hello.GroovyScriptManager; |
| 124 | +import com.litongjava.tio.http.server.handler.SimpleHttpRoutes; |
| 125 | + |
| 126 | +@BeforeStartConfiguration |
| 127 | +public class HttpServerRequestHandlerConfig { |
| 128 | + |
| 129 | + @AInitialization |
| 130 | + public void httpRoutes() { |
| 131 | + SimpleHttpRoutes simpleHttpRoutes = new SimpleHttpRoutes(); |
| 132 | + simpleHttpRoutes.add("/hi", (request) -> { |
| 133 | + String filename = "groovy/web_hello.groovy"; |
| 134 | + return GroovyScriptManager.executeClasspathScript(filename); |
| 135 | + }); |
| 136 | + |
| 137 | + TioBootServer.me().setHttpRoutes(simpleHttpRoutes); |
| 138 | + } |
| 139 | +} |
| 140 | +``` |
| 141 | + |
| 142 | +这样,每当有 HTTP 请求到达 `/hi` 路由时,将执行 `web_hello.groovy` 脚本,并返回处理结果。这使得 Groovy 脚本能够在 Web 服务中处理请求 |
| 143 | + |
| 144 | +#### 测试返回 |
| 145 | + |
| 146 | +response |
| 147 | + |
| 148 | +``` |
| 149 | +{"data":"select * from test_data where name = ?","ok":true,"code":1,"msg":null} |
| 150 | +``` |
0 commit comments