Spring 状态机注释@WithStateMachine 不工作

Spring state machine annotation @WithStateMachine not working

我刚刚在使用spring 状态机@WithStateMachine 时遇到问题。

@WithStateMachine 当我在由@EnableStateMachine 注释的 class 中定义的内部 class 上使用它时才工作,但是当我在其他地方定义 class 时它似乎不起作用. 这是我的代码:

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

@Override
public void configure(StateMachineTransitionConfigurer<States, Events> transitions)
    throws Exception {
    transitions
        .withExternal()
        .source(States.UNPAID).target(States.WAITING_FOR_RECEIVE)
        .event(Events.PAY)
        .and()
        .withExternal()
        .source(States.WAITING_FOR_RECEIVE).target(States.DONE)
        .event(Events.RECEIVE);
}

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

@WithStateMachine
public class Action {
    private Logger logger = LoggerFactory.getLogger(getClass());

    @OnTransition(target = "UNPAID")
    public void create() {
        logger.info("UNPAID");
    }

    @OnTransition(source = "UNPAID", target = "WAITING_FOR_RECEIVE")
    public void pay() {
        logger.info("WAITING_FOR_RECEIVE");
    }

    @OnTransition(source = "WAITING_FOR_RECEIVE", target = "DONE")
    public void receive() {
        logger.info("DONE");
    }
}}

但是当我在另一个 class 文件中定义 Action 时,它不起作用

我的 pom

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-messaging</artifactId>
            <version>1.2.3.RELEASE</version>
        </dependency><dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.3.3.RELEASE</version>
        </dependency>

@WithStateMachine 是具有 spring @Component 的元注释。检查您的 Action class 组件是否已扫描或手动创建为 @Bean.

然后它存在于应用程序上下文中,状态机可以找到它。