【问题标题】:Spring State Machine - Attaching static data to statesSpring State Machine - 将静态数据附加到状态
【发布时间】:2019-03-04 22:42:07
【问题描述】:

使用 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());
}

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

我不知道是否有可能以某种方式做到。

【问题讨论】:

    标签: spring-statemachine


    【解决方案1】:

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

    StateContext 就像状态机的当前快照 - 它通过各种方法和回调传递,包括动作和守卫。

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

    您可以从StateContext 获取ExtendedState

        context.getExtendedState()
            .getVariables().put("mykey", "myvalue");
    

    当它作为上下文的一部分传递时,您可以在每个动作、转换、保护等中访问ExtendedStateStateMachine 对象本身也有一个getExtendedState() 方法。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-23
      • 2020-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多