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

khe*_*ner 0 configuration factory spring-statemachine

我想包含StateMachine使用StateMachineFactory. 但@EnableStateMachineFactory注释允许命名工厂。如何命名每个 Config (即扩展EnumStateMachineConfigurerAdapter)?

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

Jan*_*hti 5

将这些enable注释与 Spring“@Configuration”类一起简单地定义在应用程序上下文中注册的 bean 名称。最容易用例子来解释:

@EnableStateMachine
Run Code Online (Sandbox Code Playgroud)

StateMachine作为豆子stateMachine

@EnableStateMachine(name = "fooMachine")
Run Code Online (Sandbox Code Playgroud)

StateMachine作为豆子fooMachine

@EnableStateMachine(name = {StateMachineSystemConstants.DEFAULT_ID_STATEMACHINE, "fooMachine"})
Run Code Online (Sandbox Code Playgroud)

StateMachine作为stateMachine带有 bean 别名的bean fooMachine

@EnableStateMachineFactory
Run Code Online (Sandbox Code Playgroud)

StateMachineFactory作为豆子stateMachineFactory

@EnableStateMachineFactory(name = "fooMachineFactory")
Run Code Online (Sandbox Code Playgroud)

StateMachineFactory作为豆子fooMachineFactory

@EnableStateMachineFactory(name = {StateMachineSystemConstants.DEFAULT_ID_STATEMACHINEFACTORY, "fooMachineFactory"})
Run Code Online (Sandbox Code Playgroud)

StateMachineFactory作为stateMachineFactory带有 bean 别名的bean fooMachineFactory

除此之外,@Configuration类的名称(扩展 StateMachineConfigurerAdapter)并不重要。在Spring中,@Configuration类也被创建为bean,这意味着下面的类将作为bean存在于Spring应用程序上下文中myConfig.MachineFactoryConfig。在 Spring 中只需要记住一件事,因为错误命名的类可能会导致 bean 覆盖!

public class MyConfig {
  @Configuration
  @EnableStateMachineFactory
  public static class MachineFactoryConfig extends StateMachineConfigurerAdapter<String, String> {
  }
}
Run Code Online (Sandbox Code Playgroud)

我刚刚在文档State Machine IDmachineId中添加了一个新部分。(仅在快照版本中,直到我们发布下一个版本为止)