Activiti ServiceTask 接受Spring bean

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xvshu/article/details/86638332

当前做流程完成回调的功能,发现普通的 activiti:class 是不能自动注入spring bean的,类似如下的配置:

在网络上找到了相关解决方案,就是用activiti:expression 替换class,这样通过表达式加载的都是自定义的类,是从spring中加载的

第一个参数,execution是流程引擎的上下文,productApi是spring bean的id,web界面配置为:

监听类如下写

package com.xs.framework.activiti.tasks;

import com.xs.backProductFactory.api.product.ProductApi;
import com.xs.backProductFactory.common.enums.ProductStatusEnum;
import com.xs.backProductFactory.dto.ResponseResult;
import com.xs.backProductFactory.dto.product.ProductDto;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.Expression;
import org.activiti.engine.delegate.JavaDelegate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import java.io.Serializable;
import java.util.Date;

/**
 * Created by xvshu on 2019/1/22 0022.
 */
@Service("productFactoryNewProductTask")
public class ProductFactoryNewProductTask implements Serializable {
    private static Logger logger = LoggerFactory.getLogger(ProductFactoryNewProductTask.class);


    public void execute(DelegateExecution delegateExecution,ProductApi productApi) throws Exception {
        logger.info("ProductFactoryNewProductTask begin!");
        Boolean val = (Boolean)delegateExecution.getVariable("pass");
//        Boolean val = (Boolean) pass.getValue(delegateExecution);
        logger.info("=ProductFactoryNewProductTask=>business:{} prossId:{} pass:{}",delegateExecution.getBusinessKey(),delegateExecution.getProcessInstanceId(),val.toString());

        //修改产品状态为审核中
        ProductDto pactDto = new ProductDto();
        pactDto.setId(Long.valueOf(delegateExecution.getBusinessKey()));
        pactDto.setStatus(ProductStatusEnum.product_status__publishing.getStatus());
        ResponseResult<Integer> responseResult = productApi.update(pactDto);

        logger.info("ProductFactoryNewProductTask end!");

    }
}

亲测有效,有需要的直接拿走!

猜你喜欢

转载自blog.csdn.net/xvshu/article/details/86638332