You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让Spring State Machine使用自定义StateContext实现类

配置Spring State Machine使用自定义StateContext实现类

核心原理

Spring State Machine默认通过DefaultStateContextFactory创建DefaultStateContext实例,要替换为自定义实现,需自定义StateContext工厂类,让它生成你的AdditionalMethodContext对象,再将该工厂注册到容器或状态机构建器中。

步骤1:实现自定义StateContextFactory

创建继承自DefaultStateContextFactory的工厂类,重写创建StateContext的方法,返回你的自定义实现:

public class AdditionalMethodContextFactory<S, E> extends DefaultStateContextFactory<S, E> {

    @Override
    public StateContext<S, E> createStateContext(Stage stage, Message<E> message,
                                                 MessageHeaders messageHeaders, ExtendedState extendedState,
                                                 Transition<S, E> transition, StateMachine<S, E> stateMachine,
                                                 State<S, E> source, State<S, E> target, Exception exception) {
        // 返回自定义的AdditionalMethodContext实例
        return new AdditionalMethodContext<>(stage, message, messageHeaders, extendedState, transition,
                stateMachine, source, target, exception);
    }
}

步骤2:注册自定义工厂到Spring容器(注解配置场景)

在你的状态机配置类中,将自定义工厂声明为Spring Bean,框架会自动使用它替换默认工厂:

@Configuration
@EnableStateMachine
public class CustomStateMachineConfig extends StateMachineConfigurerAdapter<String, String> {

    // 注册自定义StateContext工厂
    @Bean
    public StateContextFactory<String, String> stateContextFactory() {
        return new AdditionalMethodContextFactory<>();
    }

    // 常规状态机配置示例
    @Override
    public void configure(StateMachineStateConfigurer<String, String> states) throws Exception {
        states
            .withStates()
                .initial("INIT")
                .state("PROCESSING")
                .end("COMPLETED");
    }

    @Override
    public void configure(StateMachineTransitionConfigurer<String, String> transitions) throws Exception {
        transitions
            .withExternal()
                .source("INIT")
                .target("PROCESSING")
                .event("START")
                .and()
            .withExternal()
                .source("PROCESSING")
                .target("COMPLETED")
                .event("FINISH");
    }
}

步骤3:手动构建状态机时的配置(非Spring容器场景)

如果是通过StateMachineBuilder手动构建状态机,直接在配置阶段指定自定义工厂:

// 以String类型的状态和事件为例
StateMachine<String, String> stateMachine = StateMachineBuilder.builder()
    .configureStates()
        .withStates()
            .initial("INIT")
            .state("PROCESSING")
            .end("COMPLETED")
        .and()
    .configureTransitions()
        .withExternal()
            .source("INIT")
            .target("PROCESSING")
            .event("START")
        .and()
    // 配置自定义StateContext工厂
    .configureContext()
        .withStateContextFactory(new AdditionalMethodContextFactory<>())
        .and()
    .build();

验证自定义Context生效

在状态监听器或状态动作中,可将StateContext强转为自定义类型调用扩展方法:

stateMachine.addStateListener(new StateListenerAdapter<String, String>() {
    @Override
    public void stateEntered(State<String, String> state) {
        StateContext<String, String> context = stateMachine.getStateContext();
        if (context instanceof AdditionalMethodContext) {
            ((AdditionalMethodContext<String, String>) context).printParam("进入状态:" + state.getId());
        }
    }
});

内容的提问来源于stack exchange,提问作者user3333720

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 20:10:45