Spring StateMachine配置:使用@EnableSateMachineFactory时命名StateMachine

时间:2016-12-07 16:00:47

标签: configuration factory spring-statemachine

我想使用StateMachine为动态实例化添加几个StateMachineFactory配置。但是@EnableStateMachineFactory注释允许命名工厂。如何为每个配置命名(即扩展EnumStateMachineConfigurerAdapter)?

否则,如果可能的话,在配置定义中有一个如何使用setMachineID方法的示例会很有用。

1 个答案:

答案 0 :(得分:3)

使用Spring“@Configuration”类进行这些enable注释只需定义在应用程序上下文中注册的bean名称。最容易用例子解释:

@EnableStateMachine

StateMachine作为bean stateMachine

@EnableStateMachine(name = "fooMachine")

StateMachine作为bean fooMachine

@EnableStateMachine(name = {StateMachineSystemConstants.DEFAULT_ID_STATEMACHINE, "fooMachine"})

StateMachine作为bean stateMachine,其中包含bean别名fooMachine

@EnableStateMachineFactory

StateMachineFactory作为bean stateMachineFactory

@EnableStateMachineFactory(name = "fooMachineFactory")

StateMachineFactory作为bean fooMachineFactory

@EnableStateMachineFactory(name = {StateMachineSystemConstants.DEFAULT_ID_STATEMACHINEFACTORY, "fooMachineFactory"})

StateMachineFactory作为bean stateMachineFactory,其中包含bean别名fooMachineFactory

除此之外,@Configuration类(扩展StateMachineConfigurerAdapter)的名称无关紧要。在Spring中思考一个@Configuration类也被创建为bean,这意味着下面的类将作为bean myConfig.MachineFactoryConfig存在于Spring Application Context中。在Spring中只记得一件事,因为命名错误的类可能导致bean覆盖!

public class MyConfig {
  @Configuration
  @EnableStateMachineFactory
  public static class MachineFactoryConfig extends StateMachineConfigurerAdapter<String, String> {
  }
}

machineId的内容我刚向文档State Machine ID添加了一个新部分。 (仅在快照构建中,直到我们获得下一个版本)