Spring基于状态机squirrel-foundation简单使用

上一篇文章介绍对于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
  • 0
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值