上一篇文章介绍对于mybatis枚举映射器的使用,这次就要用到。
squirrel-foundation的一些使用方法在百度上资料还是比较少,我是根据以下三个大佬写的文章借鉴的,在这里记录一下。
1、squirrel-foundation-demo
2、Squirrel使用(中文文档)
3、squirrel-foundation状态机的使用细节
我在这里直接粘贴代码,便于自己之后理解。
/**
* 通过Spring创建StateMachineBuilder实例,通过buidler创建状态机(单例)
* 创建无类型化状态机,简化状态机,防止过多泛化导致代码不易阅读(因为不太理解一些高级的使用)
*/
public abstract class AbstractStateMachineEngine <T extends UntypedStateMachine> implements ApplicationContextAware {
private ApplicationContext applicationContext;
protected UntypedStateMachineBuilder stateMachineBuilder = null;
@SuppressWarnings("unchecked")
public AbstractStateMachineEngine() {
//识别泛型参数
Class<T> genericType = (Class<T>) GenericTypeResolver.resolveTypeArgument(getClass(),
AbstractStateMachineEngine.class);
stateMachineBuilder = StateMachineBuilderFactory.create(genericType, ApplicationContext.class);
}
//注入applicationContext,并在创建StateMachine实例时注入
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
/**
* 可以通过向OrderContext 上下文传递一些业务参数,比如orderId等等
*/
public boolean fire(EOrderEvents event, OrderContext context) {
T stateMachine = stateMachineBuilder.newUntypedStateMachine(
context.geteOrder().getOrderStatus(),
applicationContext);
//由于StateMachine实例不是由Spring容器创建,所以这个过程中无法通过注解方式开启事务(Spring没有机会去创建事务代理),因此采用了编程式事务
DataSourceTrans