分分钟 集成 activiti与ssm框架

集成Activiti框架 

Spring框架叫做项目(资源)的管家。如果待集成项目中,有Spring框架,那么主要就是和“Spring框架”做集成。把Activiti框架的“核心类”交给Spring管理;

Activiti的核心类,引擎配置对象,引擎对象,三大服务对象

如果框架有事务,也要把“事务”交给Spring管理。

需要集成ssm框架的点击我的文章:https://blog.csdn.net/weixin_42560234/article/details/84726974

集成步骤:

1、导入activiti jar包

activiti-5.16.4\wars\activiti-rest\WEB-INF\lib\activiti*.jar

mybatis-3.2.5.jar (尽量使用高版本)

joda-time-2.1.jar

有可能会缺失其他包,抛出异常ClassNotFound,差什么就引入什么?

如果使用的是Maven,则到中央仓库,搜索Activiti,拷贝pom的的代码端即可

2、添加Activiti配置文件-applicationContext-activiti.xml-配置核心对象

Activiti用户手册中,第5章,前两节
  

<bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">
    <property name="dataSource" ref="dataSource" />
    <property name="transactionManager" ref="transactionManager" />
    <property name="databaseSchemaUpdate" value="true" />
</bean>

     
<bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean">
    <property name="processEngineConfiguration" ref="processEngineConfiguration" />
</bean>

<bean id="repositoryService" factory-bean="processEngine" 
    factory-method="getRepositoryService" />

<bean id="runtimeService" factory-bean="processEngine"
    factory-method="getRuntimeService" />

<bean id="taskService" factory-bean="processEngine"
    factory-method="getTaskService" />

<bean id="formService" factory-bean="processEngine"
    factory-method="getFormService" />

3、把Activiti配置文件-applicationContext-activiti.xml集成到Spring

4、测试

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/applicationContext.xml")
public class ActivitiEnvTest {
    @Autowired
    private RepositoryService repositoryService;

    @Test
    public void test() {
    System.out.println(repositoryService);
    }
}

5、流程定义菜单实现

5.1 创建菜单

      在流程管理下面创建一个流程定义管理的二级菜单 就有url

5.2 创建一个WorkFlowController要有一个跳转到流程定义管理界面的方法

      该方法的映射地址就是菜单里面的url

5.3 创建流程管理界面

      拷贝其他页面修改为流程管理页面

5.4.添加流程

      先让后台能够接受来自前台的数据(/workflow/newDeploy)

      编写IWorkFlowService完成项目的部署

注意1:

直接在控制器中注入工作流相关服务完成流程部署,还是自己写一个服务进行封装,这样在Controller里面就调用一句就搞定。

注意2:

     编写部署方法时,不要传入页面层的API,

     MutilpartFile而要传入InputStream 流.

注意3:

      通过数据库信息查看是否部署成功。

//跳转到流程定义管理页面
@RequestMapping("/processDefinition/index")
public String processDefiniton() {
    //流程定义管理界面路径,默认加上前缀和后缀
    return "workFlow/processDefinition";
}

/**
* 参数:
*    name:部署名称
*    processFile:类型是MultipartFile,是SPringmvc用于文件上传接收的专用类
* 返回值:
*    AJaxResult转换结果
* @return
*/
@RequestMapping("/newDeploy")
@ResponseBody
public AjaxResult newDeploy(String name,MultipartFile processFile) {
    System.out.println(name);
    System.out.println(processFile);
    //1 不要直接注入RepositoryService来完成部署,应该交给对应Service去做
    //2 MultipartFile就是web层api,如果我们直接传入,Service层就强依赖于web层API.也就是咱们这个Service必须在web才能运行,不能强依赖.
    try {
        workFlowService.newDeploy(name,processFile.getInputStream());
        return new AjaxResult();
    } catch (IOException e) {
        e.printStackTrace();
        return new AjaxResult("部署失败!"+e.getMessage(),-1);
    }
}

@Override
public void newDeploy(String name, InputStream inputStream) {
    //完成真正部署
    //1获取引擎对象
    //2获取服务
    //3做事情
    ZipInputStream zis = null;
    try {
        zis = new ZipInputStream(inputStream);
        //创建配置对象
        DeploymentBuilder builder = repositoryService.createDeployment();
        //进行配置
        builder.name(name)//部署名称
        .addZipInputStream(zis);  //bpmn和png
        //部署
        builder.deploy();
    } catch (Exception e) {
        e.printStackTrace();
    }finally{
        if (zis != null) {
            try {
                zis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

6、运行时管理-启动流程

后台:

@RequestMapping("/startProcess")
@ResponseBody
public AjaxResult startProcess(Long businessObjId){
    try {
        service.startProcess(businessObjId);
        return new AjaxResult("启动成功");
    } catch (Exception e) {
        e.printStackTrace();
        return new AjaxResult("启动失败",-2);
    }
}

     
@Override
public void startProcess(Long businessObjId) {
    //业务逻辑:
    //状态值修改为报备中
    Customer customer = dao.get(businessObjId);
    //1是报备中
    customer.setStatus(1);
    dao.update(customer);
    Map<String, Object> variables = new HashMap<>();
    //通过客户获取营销任务:设置变量
    String seller = customer.getSeller().getNickname();
    variables.put("seller", seller);
    //启动流程实例
    //约定大于配置:
    String processDefinitionKey=ProcessInstanceUtils.getProcessDefinitionKey(Customer.class);
    definitionService.startProcess(processDefinitionKey, variables);
}

猜你喜欢

转载自blog.csdn.net/weixin_42560234/article/details/84726557
今日推荐