StateMachine - 对状态变化的操作

StateMachine - actions on states change

我使用 Spring 指南编写了状态机的实现。

虽然状态本身改变成功了,但我无法对改变状态做出任何反应。也许我误解了Beans的目标class?我需要实现状态变化时自动执行closeDoor()和startMoving()方法

控制台方法中的这些消息未显示:

import org.springframework.statemachine.annotation.OnTransition;
import org.springframework.statemachine.annotation.WithStateMachine;

@WithStateMachine
public class Beans {

  @OnTransition(target = "CLOSED_DOOR")
  void closeDoor() {
      System.out.println("closeDoor method");
  }

  @OnTransition(target = "GOING")
  void startMoving() {
      System.out.println("startMoving method");
  }
}

配置:

import org.springframework.statemachine.config.EnableStateMachine;
import org.springframework.statemachine.config.EnumStateMachineConfigurerAdapter;
import org.springframework.statemachine.config.builders.StateMachineStateConfigurer;
import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer;;

import java.util.EnumSet;


@org.springframework.context.annotation.Configuration
@EnableStateMachine
public class Configuration extends EnumStateMachineConfigurerAdapter<States, Events> {
    @Override
    public void configure(StateMachineStateConfigurer<States, Events> states)
            throws Exception {
        states
                .withStates()
                .initial(States.STAY)
                .states(EnumSet.allOf(States.class));
    }

    @Override
    public void configure(StateMachineTransitionConfigurer<States, Events> transitions)
            throws Exception {
        transitions
                .withExternal()
                .source(States.STAY).target(States.CLOSED_DOOR)
                .event(Events.CLOSE_DOOR)
                .and()
                .withExternal()
                .source(States.CLOSED_DOOR).target(States.GOING)
                .event(Events.MOVE);
    }
}

以及启动(根据事件,状态在控制台中显示正确):

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.statemachine.StateMachine;

public class App {
    @Autowired
    static StateMachine<States, Events> stateMachine;

    public static void main(String[] args) {

        stateMachine = Builder.getMachine();
        stateMachine.start();

        stateMachine.sendEvent(Events.CLOSE_DOOR);
        System.out.println(stateMachine.getState()); // ObjectState [getIds()=[CLOSED_DOOR]

        stateMachine.sendEvent(Events.MOVE);
        System.out.println(stateMachine.getState()); // ObjectState [getIds()=[GOING]
    }

依赖只有一个

<dependency>
    <groupId>org.springframework.statemachine</groupId>
    <artifactId>spring-statemachine-core</artifactId>
    <version>2.1.3.RELEASE</version>
</dependency>

我做错了什么?

我终于成功了。您的问题是 Spring DI 机制。您尝试使用 @WithStateMachine 启用转换侦听器,但后来您使用 .getMachine() 创建机器对象。它不会那样工作,您需要决定是否要使用 Spring 上下文。我已经使用上下文创建了一个解决方案,但您也可以保留它并仅使用手动构建器,但是您需要更改您的侦听器以使用手动方法而不是 Spring 上下文注释。

将您的主要 class 更改为:

public class App {

    public static void main(String[] args) {

        ConfigurableApplicationContext context = new AnnotationConfigApplicationContext("machine");
        final StateMachine<States, Events> stateMachine = context.getBean(StateMachine.class);

        stateMachine.start();
        System.out.println(stateMachine.getState()); // ObjectState [getIds()=[STAY]

        stateMachine.sendEvent(Events.CLOSE_DOOR);
        System.out.println(stateMachine.getState()); // ObjectState [getIds()=[CLOSED_DOOR]

        stateMachine.sendEvent(Events.MOVE);
        System.out.println(stateMachine.getState()); // ObjectState [getIds()=[GOING]
    }
}

让我知道它是否适合您以及您是否理解它。