【问题标题】:spring-statemachine 2.3.1 - How to get a StateMachine from a StateMachineModelFactory?spring-statemachine 2.3.1 - 如何从 StateMachineModelFactory 获取 StateMachine?
【发布时间】:2019-10-15 09:40:50
【问题描述】:

我在项目中使用 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 文件加载状态机的情况。

亲切的问候,

尼古拉斯·杜米尼

【问题讨论】:

    标签: spring-statemachine


    【解决方案1】:

    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找到。

    【讨论】:

      【解决方案2】:

      扩展StateMachineConfigurerAdapter 的配置是对自动配置的 Spring 状态机的“改编”或修改。

      为了让您的配置在 spring 中被拾取,您需要使用 @Configuration 对其进行注释。

      您还必须启用状态机自动配置,这与 @EnableStateMachie 注释一起发生。

      @Configuration
      @EnableStateMachine
      public class MyStateMachineConfig extends StateMachineConfigurerAdapter<String, String> {
      

      您可以参考the official documentation了解更多详情。

      一旦激活,您可以将 StateMachine 作为依赖项注入。

      【讨论】:

      • 知道了,谢谢。我看到还有一个 EnableStateMachineFactory 注释,对吧?并且可以命名它们,例如 StateMachine(name="myStateMachine") 然后 Bean @Qualifier("myStateMachine) private StateMachine myStateMachine; 对吗?
      • EnableStateMachineFactory 为状态机 (SM) 公开了一个工厂 - 您可以注入它并获取多个 SM 实例。是的 - 您可以为 Bean 指定名称或限定符 - 这是在您有多个相同 bean 类型的实例的情况下完成的,因此您可以明确(提供名称)您想要注入哪个实例 - 否则如果您有多个实例且没有限定符 Spring 不知道将哪个作为依赖注入。
      • 再次感谢您的更新。我的用例需要管理具有由状态机描述的生命周期的 JPA 实体。为了实现它,我尝试在这里使用示例:github.com/spring-projects/spring-statemachine/tree/master/…。此示例使用 Spring Data 存储库和 StateMachineJackson2RepositoryPopulatorFactoryBean 从 JSON 文件填充状态机存储库。然而,在我的例子中,状态机已经通过 Payrus 工具在 UML 中建模。我如何填充存储库?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-31
      • 2016-12-07
      • 1970-01-01
      • 2017-09-28
      • 2023-03-18
      • 1970-01-01
      相关资源
      最近更新 更多