Spring State Machine-将静态数据附加到状态 [英] Spring State Machine - Attaching static data to states

查看:293
本文介绍了Spring State Machine-将静态数据附加到状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用弹簧状态机,我们有状态和事件.我找不到任何有关在配置过程中是否可以将静态数据附加到状态的文档.

With spring state machine, we have states and events. I could not find any documentation on whether it is possible to attach static data to a state during configuration.

例如,如果存在状态S1和S2

For example, if there are the states S1 and 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)中可能很有用

If we could attach static data during the above configuration (like a java Map for example), it could be useful in the actions that are triggered (like entryAction and exitAction above)

我不知道是否可以做些什么.

I don't know if it is possible to do somehow.

推荐答案

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

This is achieved with two objects in the state machine - StateContext and ExtendedState.

StateContext 就像状态机的当前快照-通过各种方法和回调(包括动作和防护)传递.

StateContext is like a current snapshot of the State Machine - it's passed around in various methods and callbacks, including actions and guards.

ExtendedState 基本上是带有变量的映射.

ExtendedState is basically a map with variables.

您可以从StateContext获取ExtendedState:

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

作为上下文的一部分传递时,您可以在每个动作,过渡,保护等中访问ExtendedState. StateMachine 对象本身也有getExtendedState()方法.

As it is passed around as part of the context, you can access the ExtendedState in every action, transition, guard etc. The StateMachine object itself also has a getExtendedState() method.

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

This is the canonical way to pass static data around in the StateMachine.

这篇关于Spring State Machine-将静态数据附加到状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆