我发现在控制器变量注入之前没有调用 DefaultStateMachineService
说明:
com.example.statemachineDemo.restController 中构造函数的参数 1 需要一个无法找到的类型为“org.springframework.statemachine.service.DefaultStateMachineService”的 bean。
我得到了 IntelliJ 的类型警告。
为什么?
我的另一个 spring 框架实现不会喜欢这个。
我按照 https://docs.spring.io/spring-statemachine/docs/current/reference/#configuring-model 为 DefaultStateMachineService 添加 bean
package com.example.statemachineDemo;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.statemachine.config.EnableStateMachine;
import org.springframework.statemachine.config.EnableStateMachineFactory;
import org.springframework.statemachine.config.StateMachineConfigurerAdapter;
import org.springframework.statemachine.config.StateMachineFactory;
import org.springframework.statemachine.config.builders.StateMachineStateConfigurer;
import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer;
import org.springframework.statemachine.persist.StateMachineRuntimePersister;
import org.springframework.statemachine.service.DefaultStateMachineService;
import org.springframework.statemachine.service.StateMachineService;
import javax.management.timer.Timer;
@Slf4j
@Configuration
@EnableStateMachineFactory
public class StateMachineConfig extends StateMachineConfigurerAdapter<String, String> {
@Bean
public StateMachineService<String, String> defaultStateMachineService(StateMachineFactory<String, String> stateMachineFactory) {
return new DefaultStateMachineService<String, String>(stateMachineFactory);
}
@Override
public void configure(StateMachineStateConfigurer<String, String> states)
throws Exception {
states
.withStates()
.initial("S1")
.state("S2")
.state("S3");
MessageBuilder
.withPayload("E1")
.setHeader("foo", "bar")
.build().getHeaders().get("foo", String.class);
}
@Override
public void configure(StateMachineTransitionConfigurer<String, String> transitions)
throws Exception {
transitions
.withExternal()
.source("S1").target("S2").event("E1")
.and()
.withExternal()
.source("S1").target("S3").event("E2")
.and()
.withExternal()
.source("S2").target("S3").event("E3")
.and()
.withInternal()
.source("S2")
.action(timerAction())
.timer(Timer.ONE_SECOND)
.and()
.withInternal()
.source("S3")
.action(context -> {
log.info("{}", context.getTransition());
});
}
@Bean
public TimerAction timerAction() {
return new TimerAction();
}
}
package com.example.statemachineDemo;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.statemachine.config.StateMachineFactory;
import org.springframework.statemachine.service.DefaultStateMachineService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
@Slf4j
@RestController
@AllArgsConstructor
public class restController {
StateMachineFactory<String, String> factory;
DefaultStateMachineService<String, String> stateMachineFactory;
@GetMapping("test1")
public Flux<String> test1() {
val stateMachine = factory.getStateMachine("testingID");
stateMachine.start();
stateMachine.sendEvent("E1");
log.info("{}","test1");
log.info("{}",stateMachine);
return Flux.just("");
}
@GetMapping("test2")
public Flux<String> test2() {
val stateMachine = factory.getStateMachine("testingID");
stateMachine.sendEvent("E3");
log.info("{}","test2");
log.info("{}",stateMachine);
return Flux.just("");
}
@GetMapping("test3")
public Flux<String> test3() {
log.info("{}","test3");
log.info("{}",factory.getStateMachine("testingID"));
return Flux.just("");
}
}