我使用 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);
}
}
梦里花落0921
相关分类