StateMachine-对状态的更改

时间:2019-06-06 19:56:27

标签: java spring spring-statemachine

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

但是,尽管状态本身可以成功更改,但我对更改状态并没有任何反应。也许我误解了Beans类的目标?当状态改变时,我需要实现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>

我做错了什么?

1 个答案:

答案 0 :(得分:3)

最后,我设法使它起作用。您的问题是Spring DI机制。您尝试使用@WithStateMachine来启用过渡侦听器,但是随后您使用.getMachine()创建了机器对象。它不会那样工作,您需要确定是否要使用Spring Context。我已经创建了一个使用上下文的解决方案,但是您也可以保留它,而仅使用手动生成器,但是您将需要更改您的侦听器,以使用手动方法代替Spring Context批注。

将主课程更改为:

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]
    }
}

让我知道它是否对您有用以及您是否理解。