【发布时间】:2018-06-13 18:01:45
【问题描述】:
我目前正在使用 Spring 状态机构建一个小示例项目。
我的配置:
@Configuration
@EnableStateMachine
public class StateMachineConfiguration extends EnumStateMachineConfigurerAdapter<States, Events> {
@Override
public void configure(StateMachineStateConfigurer<States, Events> states)
throws Exception {
states
.withStates()
.initial(States.LOCKED)
.states(EnumSet.allOf(States.class));
}
@Override
public void configure(StateMachineTransitionConfigurer<States, Events> transitions)
throws Exception {
transitions
.withExternal()
.source(States.LOCKED)
.target(States.UNLOCKED)
.event(Events.COIN)
.and()
.withExternal()
.source(States.UNLOCKED)
.target(States.LOCKED)
.event(Events.PUSH);
}
@Bean
public StateMachineListener<States, Events> listener() {
return new StateMachineListenerAdapter<States, Events>() {
@Override
public void stateChanged(State<States, Events> from, State<States, Events> to) {
System.out.println("State change to " + to.getId());
}
};
}
}
当我现在尝试用
注入状态机时@Autowired
StateMachine<States, Events> stateMachine;
IntelliJ 给了我提示,这不能是 autowired 因为那里没有 bean。如果我运行应用程序,我也会遇到异常。
gradle 依赖:
compile 'org.springframework.statemachine:spring-statemachine-core:2.0.1.
compile group: 'org.springframework.boot', name: 'spring-boot-starter', version: '2.0.1.RELEASE'
compile group: 'org.springframework.shell', name: 'spring-shell-starter', version: '2.0.1.RELEASE'
compile group: 'org.springframework.statemachine', name: 'spring-statemachine-boot', version: '1.2.11.RELEASE'
【问题讨论】:
-
问题解决了吗?
-
@seanhawk 查看我的帖子
-
@DanielEisenreich 在代码中,如果您从
@EnableStateMachine更改为@EnableStateMachineFactory注释,那将有效。谢谢。
标签: java spring spring-boot spring-statemachine