|
| 1 | +# FlowScriptContext |
| 2 | + |
| 3 | +## 何时使用 |
| 4 | + |
| 5 | +当需要在 Groovy 脚本中访问 Spring Bean、流程记录或操作人数据时使用。`FlowScriptContext` 是脚本运行时的 `$bind` 上下文对象,由 `ScriptRuntimeContext` 在每次脚本执行时注入为 `$bind` 变量。典型场景: |
| 6 | + |
| 7 | +- 在自定义 Groovy 脚本中查询流程记录(`$bind.getRecordById(...)`) |
| 8 | +- 在脚本中获取操作人信息(`$bind.getOperatorById(...)`) |
| 9 | +- 在脚本中调用 Spring Bean(`$bind.getBean(...)`) |
| 10 | +- 需要替换脚本上下文的 Bean 工厂实现(如单元测试中注入 Mock) |
| 11 | + |
| 12 | +## 如何引用 |
| 13 | + |
| 14 | +### Maven 坐标 |
| 15 | + |
| 16 | +```xml |
| 17 | +<dependency> |
| 18 | + <groupId>com.codingapi.flow</groupId> |
| 19 | + <artifactId>flow-engine-framework</artifactId> |
| 20 | + <version>0.0.26</version> |
| 21 | +</dependency> |
| 22 | +``` |
| 23 | + |
| 24 | +## API 说明 |
| 25 | + |
| 26 | +### 核心类 |
| 27 | + |
| 28 | +| 类名 | 包路径 | 说明 | |
| 29 | +|------|--------|------| |
| 30 | +| `FlowScriptContext` | `com.codingapi.flow.script.runtime` | 脚本运行时 `$bind` 上下文单例,持有 `IBeanFactory` 委托 | |
| 31 | +| `IBeanFactory` | `com.codingapi.flow.script.runtime` | Bean 工厂接口,定义 Bean 查询、记录查询、操作人查询方法 | |
| 32 | +| `GroovyScriptBind` | `com.codingapi.flow.script.request` | Groovy 脚本绑定对象,包装 `FlowScriptContext` 并作为 `$bind` 注入脚本 | |
| 33 | + |
| 34 | +### 关键方法 |
| 35 | + |
| 36 | +#### FlowScriptContext |
| 37 | + |
| 38 | +| 方法签名 | 参数说明 | 返回值 | 说明 | |
| 39 | +|----------|----------|--------|------| |
| 40 | +| `getInstance()` | 无 | `FlowScriptContext` | 获取单例实例 | |
| 41 | +| `setBeanFactory(IBeanFactory)` | `beanFactory` — Bean 工厂实现 | `void` | 设置自定义 Bean 工厂(由 Starter 自动配置或测试手动注入) | |
| 42 | +| `getBean(Class<T>)` | `clazz` — Bean 类型 | `T` | 按类型获取 Spring Bean | |
| 43 | +| `getBean(String, Class<T>)` | `name` — Bean 名称, `clazz` — Bean 类型 | `T` | 按名称和类型获取 Spring Bean | |
| 44 | +| `getBeans(Class<T>)` | `clazz` — Bean 类型 | `List<T>` | 按类型获取所有 Spring Bean | |
| 45 | +| `getRecordById(long)` | `id` — 记录 ID | `FlowRecord` | 根据 ID 获取流程记录 | |
| 46 | +| `getOperatorById(long)` | `userId` — 操作人 ID | `IFlowOperator` | 根据 ID 获取操作人 | |
| 47 | +| `findOperatorsByIds(List<Long>)` | `ids` — 操作人 ID 列表 | `List<IFlowOperator>` | 批量获取操作人 | |
| 48 | + |
| 49 | +#### IBeanFactory 接口默认实现 |
| 50 | + |
| 51 | +| 方法 | 默认行为 | 说明 | |
| 52 | +|------|----------|------| |
| 53 | +| `getBean(Class<T>)` | 返回 `null` | 需由 Spring 环境实现覆盖 | |
| 54 | +| `getBean(String, Class<T>)` | 返回 `null` | 需由 Spring 环境实现覆盖 | |
| 55 | +| `getBeans(Class<T>)` | 返回 `null` | 需由 Spring 环境实现覆盖 | |
| 56 | +| `getRecordById(long)` | 返回 `null` | 需由 Spring 环境实现覆盖 | |
| 57 | +| `getOperatorById(long)` | 委托给 `GatewayContext` | 默认通过网关上下文获取操作人 | |
| 58 | +| `findOperatorsByIds(List<Long>)` | 委托给 `GatewayContext` | 默认通过网关上下文批量获取 | |
| 59 | + |
| 60 | +## 使用示例 |
| 61 | + |
| 62 | +```groovy |
| 63 | +// 在 Groovy 脚本中使用 $bind(脚本由 ScriptRuntimeContext 自动注入) |
| 64 | +def run(request) { |
| 65 | + // 查询流程记录 |
| 66 | + def record = $bind.getRecordById(request.getRecordId()) |
| 67 | +
|
| 68 | + // 获取操作人信息 |
| 69 | + def operator = $bind.getOperatorById(record.getCurrentOperatorId()) |
| 70 | +
|
| 71 | + // 调用自定义 Spring Bean |
| 72 | + def notifyService = $bind.getBean(com.example.NotifyService) |
| 73 | + notifyService.send(operator.getEmail(), "您有一条待办") |
| 74 | +
|
| 75 | + return 'PASS' |
| 76 | +} |
| 77 | +``` |
| 78 | + |
| 79 | +```java |
| 80 | +// Spring Boot 自动配置中注册(flow-engine-starter 内部调用) |
| 81 | +@Component |
| 82 | +public class FlowScriptContextRegister implements InitializingBean { |
| 83 | + |
| 84 | + private final ApplicationContext applicationContext; |
| 85 | + private final FlowOperatorGateway flowOperatorGateway; |
| 86 | + private final FlowRecordRepository flowRecordRepository; |
| 87 | + |
| 88 | + @Override |
| 89 | + public void afterPropertiesSet() { |
| 90 | + FlowScriptContext.getInstance().setBeanFactory(new IBeanFactory() { |
| 91 | + @Override |
| 92 | + public <T> T getBean(Class<T> clazz) { |
| 93 | + return applicationContext.getBean(clazz); |
| 94 | + } |
| 95 | + @Override |
| 96 | + public <T> T getBean(String name, Class<T> clazz) { |
| 97 | + return applicationContext.getBean(name, clazz); |
| 98 | + } |
| 99 | + @Override |
| 100 | + public <T> List<T> getBeans(Class<T> clazz) { |
| 101 | + return new ArrayList<>(applicationContext.getBeansOfType(clazz).values()); |
| 102 | + } |
| 103 | + @Override |
| 104 | + public FlowRecord getRecordById(long id) { |
| 105 | + return flowRecordRepository.getFlowRecord(id); |
| 106 | + } |
| 107 | + }); |
| 108 | + } |
| 109 | +} |
| 110 | +``` |
| 111 | + |
| 112 | +```java |
| 113 | +// 单元测试中手动注入 |
| 114 | +FlowScriptContext.getInstance().setBeanFactory(new IBeanFactory() { |
| 115 | + @Override |
| 116 | + public FlowRecord getRecordById(long id) { |
| 117 | + return testRecordRepository.getFlowRecord(id); |
| 118 | + } |
| 119 | +}); |
| 120 | +``` |
| 121 | + |
| 122 | +## 注意事项 |
| 123 | + |
| 124 | +- **注入机制**:`FlowScriptContext` 本身不执行查询逻辑,全部委托给 `IBeanFactory`。生产环境由 `FlowScriptContextRegister` 在 Spring 启动时自动注入,测试环境需手动设置 |
| 125 | +- **`$bind` 绑定**:`GroovyScriptBind` 包装 `FlowScriptContext` 实例,由 `ScriptRuntimeContext.execute()` 自动注入为脚本中的 `$bind` 变量,无需手动创建 |
| 126 | +- **线程安全性**:单例使用饿汉式初始化,`setBeanFactory()` 无同步保护,应在应用启动阶段完成设置 |
| 127 | +- **默认实现**:`IBeanFactory` 的操作人查询方法默认委托给 `GatewayContext`,而 Bean 和记录查询默认返回 `null`,必须由 Spring 环境覆盖 |
| 128 | +- **Groovy 脚本中的使用**:脚本内直接使用 `$bind` 关键字,无需 import 或显式获取实例 |
0 commit comments