前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >spring-state-machine守卫

spring-state-machine守卫

作者头像
阿超
发布2023-11-05 10:07:07
1790
发布2023-11-05 10:07:07
举报
文章被收录于专栏:快乐阿超

是非之声,无翼而飞;损益之名,无胫而走。——白居易

文档:

https://docs.spring.io/spring-statemachine/docs/current/reference/#configuring-guards

说白了守卫是用来判断事件执行后能否更新到下一个状态的

这里按之前提到的代码示例来示范

首先是配置为返回true,发现下面一路正常打印

然后是return false

代码语言:javascript
复制
package com.ruben.parent;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.statemachine.StateMachine;
import org.springframework.statemachine.config.EnableStateMachineFactory;
import org.springframework.statemachine.config.EnumStateMachineConfigurerAdapter;
import org.springframework.statemachine.config.StateMachineBuilder;
import org.springframework.statemachine.config.builders.StateMachineConfigurationConfigurer;
import org.springframework.statemachine.config.builders.StateMachineStateConfigurer;
import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer;
import org.springframework.statemachine.listener.StateMachineListener;
import org.springframework.statemachine.listener.StateMachineListenerAdapter;
import org.springframework.statemachine.state.State;

@Configuration
@EnableStateMachineFactory
public class StateMachineConfig extends EnumStateMachineConfigurerAdapter<States, Events> {

    @Override
    public void configure(StateMachineConfigurationConfigurer<States, Events> config) throws Exception {
        config
                .withConfiguration()
                .autoStartup(true)
                .listener(listener());
    }

    @Override
    public void configure(StateMachineStateConfigurer<States, Events> states) throws Exception {
        states
                .withStates()
                .initial(States.READY)
                .state(States.GAME, gameStateMachine())
                .and()
                .withStates()
                .parent(States.GAME)
                .initial(States.NIGHT)
                .state(States.DAY);
    }

    @Override
    public void configure(StateMachineTransitionConfigurer<States, Events> transitions) throws Exception {
        transitions
                .withExternal()
                .source(States.READY).target(States.GAME).event(Events.START_GAME).guard(context->{
                    return true;
                })
                .and()
                .withExternal()
                .source(States.NIGHT).target(States.DAY).event(Events.DAY_COMES)
                .and()
                .withExternal()
                .source(States.DAY).target(States.NIGHT).event(Events.NIGHT_COMES);
    }

    @Bean
    public StateMachine<States, Events> gameStateMachine() {
        StateMachineBuilder.Builder<States, Events> builder = StateMachineBuilder.builder();
        try {
            builder.configureStates()
                    .withStates()
                    .initial(States.NIGHT)
                    .state(States.DAY);
            builder.configureTransitions()
                    .withExternal()
                    .source(States.NIGHT).target(States.DAY).event(Events.DAY_COMES)
                    .and()
                    .withExternal()
                    .source(States.DAY).target(States.NIGHT).event(Events.NIGHT_COMES);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return builder.build();
    }

    @Bean
    public StateMachineListener<States, Events> listener() {
        return new StateMachineListenerAdapter<States, Events>() {
            @Override
            public void stateChanged(State<States, Events> from, State<States, Events> to) {
                System.out.println("State change from " + (from != null ? from.getId() : "null") + " to " + to.getId());
            }
        };
    }
}

这里的守卫是org.springframework.statemachine.guard.Guard这个类,也可以使用spel表达式,将guard函数改为guardExpression即可

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2023-11-04,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档