springboot2.0集成activiti6.0

    activiti6.0直接使用starter集成到springboot2.0时,会报异常,无法直接进行集成。所以我才用spring-activiti来集成activiti6.0.

整体文件结构如下:

springboot2.0集成activiti6.0_第1张图片

1.pom文件依赖


  4.0.0
  com.stu
  activitiApplication
  0.0.1-SNAPSHOT
  
		org.springframework.boot
		spring-boot-starter-parent
		2.0.3.RELEASE
		 
  
  
    	
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.3.2
        
        
	        mysql
	        mysql-connector-java
               
          
            com.alibaba  
            druid  
            1.0.19  
        
        
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
        
        
            org.activiti
            activiti-spring
            6.0.0
              
      

我采用的是mysql5.7.22,如果parent才用2.0.4版会导致连接数据库出现问题,所有我才用2.0.3(也可以采用2.0.1或者2.0.2版本).

2.bpmn文件配置springboot默认从classpath的processes文件下读取,所以必须建该文件夹。示例流程图如下:

springboot2.0集成activiti6.0_第2张图片

3.properties文件配置:

server.port=8080
server.servlet.context-path=/activiti

mybatis.mapper-locations=classpath:mapper/*.xml 

spring.datasource.url=jdbc:mysql://localhost:3306/stu 
spring.datasource.username = root  
spring.datasource.password = password  
spring.datasource.driver-class-name = com.mysql.jdbc.Driver

4.启动类:

package com.stu;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.stu.mapper")
public class ActivitiApplication {
	public static void main(String[] args) {
		SpringApplication.run(ActivitiApplication.class, args);

	}
}

5.配置类:

package com.stu.config;

import java.io.IOException;

import javax.sql.DataSource;

import org.activiti.engine.HistoryService;
import org.activiti.engine.IdentityService;
import org.activiti.engine.ManagementService;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.spring.SpringProcessEngineConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

@Configuration
public class ActivitiConfuguration {
	@Bean
    public ProcessEngine processEngine(DataSourceTransactionManager transactionManager, DataSource dataSource) throws IOException {
        SpringProcessEngineConfiguration configuration = new SpringProcessEngineConfiguration();
        //自动部署已有的流程文件
        Resource[] resources = new PathMatchingResourcePatternResolver().getResources(ResourceLoader.CLASSPATH_URL_PREFIX + "processes/*.bpmn");
        configuration.setTransactionManager(transactionManager);
        configuration.setDataSource(dataSource);
        configuration.setDatabaseSchemaUpdate("true");
        configuration.setDeploymentResources(resources);
        configuration.setDbIdentityUsed(false);
        return configuration.buildProcessEngine();
    }
 
    @Bean
    public RepositoryService repositoryService(ProcessEngine processEngine) {
        return processEngine.getRepositoryService();
    }
 
    @Bean
    public RuntimeService runtimeService(ProcessEngine processEngine) {
        return processEngine.getRuntimeService();
    }
 
    @Bean
    public TaskService taskService(ProcessEngine processEngine) {
        return processEngine.getTaskService();
    }
 
    @Bean
    public HistoryService historyService(ProcessEngine processEngine) {
        return processEngine.getHistoryService();
    }
 
    @Bean
    public ManagementService managementService(ProcessEngine processEngine) {
        return processEngine.getManagementService();
    }
 
    @Bean
    public IdentityService identityService(ProcessEngine processEngine) {
        return processEngine.getIdentityService();
    }
}
package com.stu.config;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import com.alibaba.druid.pool.DruidDataSource;

@Configuration
public class DruidConfiguration {
	
	@Autowired
	Environment env;
	/**
	 * 数据源:配置DruidDataSource
	 * @return
	 */
	@Bean(destroyMethod =  "close") 
	public DataSource dataSource() {

		DruidDataSource ds = new DruidDataSource();
		ds.setUrl(env.getProperty("spring.datasource.url"));
		ds.setDriverClassName(env.getProperty("spring.datasource.driver-class-name"));
		ds.setPassword(env.getProperty("spring.datasource.password"));
		ds.setUsername(env.getProperty("spring.datasource.username"));
		ds.setInitialSize(2);//初始化时建立物理连接的个数  
		ds.setMaxActive(20);//最大连接池数量  
		ds.setMinIdle(0);//最小连接池数量  
		ds.setMaxWait(60000);//获取连接时最大等待时间,单位毫秒。  
		ds.setValidationQuery("SELECT 1");//用来检测连接是否有效的sql  
		ds.setTestOnBorrow(false);//申请连接时执行validationQuery检测连接是否有效  
		ds.setTestWhileIdle(true);//建议配置为true,不影响性能,并且保证安全性。  
		ds.setPoolPreparedStatements(false);//是否缓存preparedStatement,也就是PSCache  
		return ds;
		
	}
	/**
	 * 事务:配置DataSourceTransactionManager
	 * @param dataSource
	 * @return
	 */
	@Bean("dataSourceTransactionManager")
	public DataSourceTransactionManager dataSourceTransactionManager(DataSource dataSource) {		
		 return new DataSourceTransactionManager(dataSource);
	}
}

6.controller控制层测试:

package com.stu.controller;

import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.Deployment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ActivitiController {
	
	@Autowired
	private RepositoryService repositoryService;
	
	@RequestMapping("/deploy")
	public String deploy() {
		
		Deployment deployment = repositoryService.createDeployment()
			.name("demoDeployment")
			.addClasspathResource("processes/Demo.bpmn")
			.addClasspathResource("processes/Demo.png")
			.deploy();
		
		System.out.println("部署Id: " + deployment.getId());
		System.out.println("部署名称: " + deployment.getName());
		
		return "部署成功";
	}
}

其实,在程序启动时springboot会自动部署。

其他封装可以参考https://blog.csdn.net/qq_33698579/article/details/80362864。

你可能感兴趣的:(springboot2.0集成activiti6.0)