在项目中使用spring-statemachine 2.3.1时,我有以下业务案例:
状态机是使用Papyrus插件定义的,并使用UmlStateMachineModelFactory从uml文件中加载,如下所示:
public class MyStateMachineConfig extends StateMachineConfigurerAdapter<String, String>
{
@Override
public void configure(StateMachineModelConfigurer<String, String> model) throws Exception
{
model.withModel().factory(myStateMachineModelFactory());
}
@Bean
public StateMachineModelFactory<String, String> myStateMachineModelFactory()
{
return new UmlStateMachineModelFactory("classpath:my.uml");
}
....我需要使用JPA将状态机上下文持久化到数据库中。为此,我需要使用StateMachinePersister.persist()。此方法使用StateMachine实例作为其第一个输入参数。但是,我无法从我的StateMachineModelFactory中获取StateMachine实例。StateMachineFactory类有一个名为getStateMachine()的方法,而StateMachineModelFactory类没有。
我也没有找到从StateMachineModelFactory实例获取StateMachineFactory实例的方法。有没有人可以提供一些建议,最好是一些例子?文档中有不同的示例来说明如何做到这一点,但是对于状态机是从UML文件加载的情况,文档中没有这样的示例。
致以亲切的问候,
Nicolas DUMINIL
发布于 2020-10-30 18:44:57
您的StateMachineFactory配置应如下所示
@Configuration
@EnableStateMachineFactory
public static class SsmConfig
extends EnumStateMachineConfigurerAdapter<States, Events> {
@Override
public void configure(StateMachineStateConfigurer<States, Events> states)
throws Exception {
states
.withStates()
.initial(States.S1)
.end(States.SF)
.states(EnumSet.allOf(States.class));
}
}然后,您可以简单地在任何地方注入StateMachineFactory并获得state machine。
class SomeService {
@Autowired
private StateMachineFactory<States, Events> factory;
void method() {
StateMachine<States,Events> stateMachine = factory.getStateMachine();
stateMachine.start();
}
}更多信息可以在here上找到。
发布于 2019-10-23 15:25:14
扩展StateMachineConfigurerAdapter的配置是对自动配置的Spring状态机的“改编”或修改。
为了让spring获取您的配置,您需要使用@Configuration对其进行注释。
您还必须启用状态机自动配置,这是通过@EnableStateMachie注释实现的。
@Configuration
@EnableStateMachine
public class MyStateMachineConfig extends StateMachineConfigurerAdapter<String, String> {有关更多详细信息,请参阅the official documentation。
一旦激活,您就可以将StateMachine作为依赖项注入。
https://stackoverflow.com/questions/58391676
复制相似问题