本文共 8233 字,大约阅读时间需要 27 分钟。
英文原文:
目录
Spring Statemachine是应用程序开发人员在Spring应用程序中使用状态机概念的框架。
Spring Statemachine旨在提供以下功能:
在项目中使用spring-statemachine的推荐方法是使用依赖关系管理系统 - 下面的代码片段可以复制并粘贴到您的构建中。 需要帮忙? 请参阅我们的Maven和Gradle构建入门指南。(导航到英文页面可以选择版本和依赖方式)
Maven
org.springframework.statemachine spring-statemachine-core 2.0.3.RELEASE
Gradle
dependencies { compile 'org.springframework.statemachine:spring-statemachine-core:2.0.3.RELEASE'}
以下示例应该了解如何配置和使用状态机。 假设我们有状态STATE1,STATE2和事件EVENT1,EVENT2。
static enum States { STATE1, STATE2}static enum Events { EVENT1, EVENT2}
public StateMachinebuildMachine() throws Exception { Builder builder = StateMachineBuilder.builder(); builder.configureStates() .withStates() .initial(States.STATE1) .states(EnumSet.allOf(States.class)); builder.configureTransitions() .withExternal() .source(States.STATE1).target(States.STATE2) .event(Events.EVENT1) .and() .withExternal() .source(States.STATE2).target(States.STATE1) .event(Events.EVENT2); return builder.build();}
StateMachinestateMachine = buildMachine();stateMachine.start();stateMachine.sendEvent(Events.EVENT1);stateMachine.sendEvent(Events.EVENT2);
@Configuration@EnableStateMachinestatic class Config1 extends EnumStateMachineConfigurerAdapter{ @Override public void configure(StateMachineStateConfigurer states) throws Exception { states .withStates() .initial(States.STATE1) .states(EnumSet.allOf(States.class)); } @Override public void c