|
此版本仍在开发中,尚未被认为是稳定版。请使用最新稳定版 Spring Shell 4.0.1! |
流
当您使用 Flow Components 构建涉及多个组件的某些功能时,您的实现可能会变得有些杂乱。为了简化这些用例,我们增加了一个可以将多个组件执行串联在一起作为“流”的 ComponentFlow。
以下列表显示了在Shell中流程及其输出的示例:
static class FlowSampleComplex {
@Autowired
private ComponentFlow.Builder componentFlowBuilder;
public void runFlow() {
List<SelectItem> single1SelectItems = Arrays.asList(SelectItem.of("key1", "value1"),
SelectItem.of("key2", "value2"));
List<SelectItem> multi1SelectItems = Arrays.asList(SelectItem.of("key1", "value1"),
SelectItem.of("key2", "value2"), SelectItem.of("key3", "value3"));
ComponentFlow flow = componentFlowBuilder.clone()
.reset()
.withStringInput("field1")
.name("Field1")
.defaultValue("defaultField1Value")
.and()
.withStringInput("field2")
.name("Field2")
.and()
.withNumberInput("number1")
.name("Number1")
.and()
.withNumberInput("number2")
.name("Number2")
.defaultValue(20.5)
.numberClass(Double.class)
.and()
.withConfirmationInput("confirmation1")
.name("Confirmation1")
.and()
.withPathInput("path1")
.name("Path1")
.and()
.withSingleItemSelector("single1")
.name("Single1")
.selectItems(single1SelectItems)
.and()
.withMultiItemSelector("multi1")
.name("Multi1")
.selectItems(multi1SelectItems)
.and()
.build();
flow.run();
}
}
| 正常组件的执行顺序与通过构建器定义的一致。 |
使用next函数有条件地选择在流程中跳转的位置,并返回目标组件id。如果返回的id为null或不存在,则流程将在那里停止。
static class FlowSampleConditional {
@Autowired
private ComponentFlow.Builder componentFlowBuilder;
public void runFlow() {
Map<String, String> single1SelectItems = new HashMap<>();
single1SelectItems.put("Field1", "field1");
single1SelectItems.put("Field2", "field2");
ComponentFlow flow = componentFlowBuilder.clone()
.reset()
.withSingleItemSelector("single1")
.name("Single1")
.selectItems(single1SelectItems)
.next(ctx -> ctx.getResultItem().get().getItem())
.and()
.withStringInput("field1")
.name("Field1")
.defaultValue("defaultField1Value")
.next(ctx -> null)
.and()
.withStringInput("field2")
.name("Field2")
.defaultValue("defaultField2Value")
.next(ctx -> null)
.and()
.build();
flow.run();
}
}
运行流程的结果返回ComponentFlowResult,您可以使用此结果进行进一步的操作。 |