Spring State Machine - Attaching static data to states

前端 未结 1 2021
离开以前
离开以前 2020-12-22 02:11

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.

1条回答
  •  感动是毒
    2020-12-22 02:39

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

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

    ExtendedState is basically a map with variables.

    You can get the ExtendedState from the StateContext:

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

    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.

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

    0 讨论(0)
提交回复
热议问题