问题描述
在我在线查找的所有示例中,StateMachine都是静态配置的
In all examples I have looked up online, the StateMachine is configured statically
@Override
public void configure(StateMachineTransitionConfigurer<BookStates, BookEvents> transitions) throws Exception {
transitions
.withExternal()
.source(BookStates.AVAILABLE)
.target(BookStates.BORROWED)
.event(BookEvents.BORROW)
.and()
.withExternal()
.source(BookStates.BORROWED)
.target(BookStates.AVAILABLE)
.event(BookEvents.RETURN)
.and()
.withExternal()
.source(BookStates.AVAILABLE)
.target(BookStates.IN_REPAIR)
.event(BookEvents.START_REPAIR)
.and()
.withExternal()
.source(BookStates.IN_REPAIR)
.target(BookStates.AVAILABLE)
.event(BookEvents.END_REPAIR);
}
我想通过从数据库中获取源,目标,事件来动态"配置StateMachine,并循环遍历列表以流体"方式进行配置.
I would like to configure the StateMachine "dynamically" by fetching the source,target,event from a Database and loop through the List to configure this in a "fluid" manner.
这可能吗?
推荐答案
是的,可以通过 StateMachineModelFactory
.您可以使用 StateMachineModelConfigurer
就像这样:
Yes it is possible through a custom implementation of StateMachineModelFactory
. You can hook it using StateMachineModelConfigurer
like so:
@Configuration
@EnableStateMachine
public static class Config1 extends StateMachineConfigurerAdapter<String, String> {
@Override
public void configure(StateMachineModelConfigurer<String, String> model) throws Exception {
model
.withModel()
.factory(modelFactory());
}
@Bean
public StateMachineModelFactory<String, String> modelFactory() {
return new CustomStateMachineModelFactory();
}
}
在实现中,您可以从外部服务动态加载SM模型所需的任何内容.以下是官方的示例doc :
In your implementation you can dynamically load whatever is needed for the SM model from external services. Below is an example from the official doc:
public static class CustomStateMachineModelFactory implements StateMachineModelFactory<String, String> {
@Override
public StateMachineModel<String, String> build() {
ConfigurationData<String, String> configurationData = new ConfigurationData<>();
Collection<StateData<String, String>> stateData = new ArrayList<>();
stateData.add(new StateData<String, String>("S1", true));
stateData.add(new StateData<String, String>("S2"));
StatesData<String, String> statesData = new StatesData<>(stateData);
Collection<TransitionData<String, String>> transitionData = new ArrayList<>();
transitionData.add(new TransitionData<String, String>("S1", "S2", "E1"));
TransitionsData<String, String> transitionsData = new TransitionsData<>(transitionData);
StateMachineModel<String, String> stateMachineModel = new DefaultStateMachineModel<String, String>(configurationData,
statesData, transitionsData);
return stateMachineModel;
}
@Override
public StateMachineModel<String, String> build(String machineId) {
return build();
}
}
您可以轻松地从数据库动态加载状态和转换,并填充 ConfigurationData
.
You can easily load the states and transitions dynamically from the DB and populate the ConfigurationData
.
这篇关于Spring StateMachine-从数据库配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!