Spring 状态机 - 将静态数据附加到状态

sat*_*sat 2 spring-statemachine

使用 Spring 状态机,我们有状态和事件。我找不到任何关于是否可以在配置期间将静态数据附加到状态的文档。

例如,如果有状态 S1 和 S2

public void configure(StateMachineStateConfigurer<String, String> states) throws Exception  {
    states.withStates()
                .initial("INIT")
                .end("END")
                .state("S1", null, exitAction())
                .state("S2", entryAction());
}
Run Code Online (Sandbox Code Playgroud)

如果我们可以在上述配置期间附加静态数据(例如 java Map),那么它在触发的操作中可能会很有用(如上面的 entryAction 和 exitAction)

我不知道是否可以采取某种方式。

hov*_*yan 5

这是通过状态机中的两个对象 - StateContextExtendedState来实现的。

StateContext就像状态机的当前快照 - 它在各种方法和回调中传递,包括操作和防护。

ExtendedState基本上是一个带有变量的映射。

ExtendedState您可以从以下位置获取StateContext

    context.getExtendedState()
        .getVariables().put("mykey", "myvalue");
Run Code Online (Sandbox Code Playgroud)

由于它作为上下文的一部分传递,因此您可以ExtendedState在每个操作、转换、防护等中访问它。StateMachine对象本身也有一个getExtendedState()方法。

这是在 StateMachine 中传递静态数据的规范方法。