Spring状态机注释@WithStateMachine不起作用

时间:2017-04-13 13:09:04

标签: java spring spring-statemachine

使用弹簧状态机@WithStateMachine时遇到问题。

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

@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");
    }
}}

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

enter image description here

我的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>

1 个答案:

答案 0 :(得分:0)

@WithStateMachine是一个有弹簧@Component的元注释。检查您的Action类是扫描组件还是手动创建为@Bean

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