activiti6.0整合springboot2.x以及bug解决

springboot在整合activiti时,会有一些官方bug。那么接下来将讲述如何springboot2.x完美整合activiti。

如果想直接看如何解决以及整合步骤不需要知道原理,请直接看末尾总结

一、引入pom文件

        
            org.activiti
            activiti-spring-boot-starter-basic
            6.0.0
        

二、配置数据源

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

至此一般来讲,应该是可以启动项目的。接下来启动试一试
发现启动出错,这里放一些出错代码

Failed to introspect annotations on class org.activiti.spring.boot.SecurityAutoConfiguration$$EnhancerBySpringCGLIB$$d26f5901

Failed to introspect annotations on class org.activiti.spring.boot.SecurityAutoConfiguration$$EnhancerBySpringCGLIB$$d26f5901

从上述代码可知是在初始化SecurityAutoConfiguration的时候出现错误,那么我们一般也不需要Security这个框架,直接将此配置类给排除掉,就可以了。

@SpringBootApplication(exclude = {org.activiti.spring.boot.SecurityAutoConfiguration.class})

我们再来启动,发现还是有问题,那么这次问题是什么呢?

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.activiti.spring.SpringProcessEngineConfiguration]

Caused by: java.io.FileNotFoundException: class path resource [processes/] cannot be resolved to URL because it does not exist

看起来好像是由于根目录下的processes文件夹没有找到而报错,顾名思义,一看这应该是activiti默认将根目录下的processes文件夹作为统一放置流程资源文件的地方。
那么如何解决呢?这里有两种解决办法

- 第一种

第一种当然很简单了,既然processes文件夹找不到,直接创建一个就好了呗

- 第二种

通过源码来分析,既然是org.activiti.spring.SpringProcessEngineConfiguration构建失败,那么继续看出错:

    at org.activiti.spring.boot.DataSourceProcessEngineAutoConfiguration$DataSourceProcessEngineConfiguration.springProcessEngineConfiguration(DataSourceProcessEngineAutoConfiguration.java:57) ~[activiti-spring-boot-starter-basic-6.0.0.jar:na]

可知org.activiti.spring.SpringProcessEngineConfiguration是由org.activiti.spring.boot.DataSourceProcessEngineAutoConfiguration来构建,那么我们在这个类中打个断点,查看构建逻辑。

  • DataSourceProcessEngineAutoConfiguration.java
        @Bean
        @ConditionalOnMissingBean
        public SpringProcessEngineConfiguration springProcessEngineConfiguration(DataSource dataSource, PlatformTransactionManager transactionManager, SpringAsyncExecutor springAsyncExecutor) throws IOException {
            return this.baseSpringProcessEngineConfiguration(dataSource, transactionManager, springAsyncExecutor);
        }

进入这个方式之后走到了AbstractProcessEngineAutoConfiguration的baseSpringProcessEngineConfiguration逻辑中

  • AbstractProcessEngineAutoConfiguration.java
    protected SpringProcessEngineConfiguration baseSpringProcessEngineConfiguration(DataSource dataSource, PlatformTransactionManager platformTransactionManager, SpringAsyncExecutor springAsyncExecutor) throws IOException {
        List procDefResources = this.discoverProcessDefinitionResources(this.resourceLoader, this.activitiProperties.getProcessDefinitionLocationPrefix(), this.activitiProperties.getProcessDefinitionLocationSuffixes(), this.activitiProperties.isCheckProcessDefinitions());
        .......
        ...
        ..
      }

查看discoverProcessDefinitionResources实现

    public List discoverProcessDefinitionResources(ResourcePatternResolver applicationContext, String prefix, List suffixes, boolean checkPDs) throws IOException {
        if (!checkPDs) {
            return new ArrayList();
        } else {
            List result = new ArrayList();
            Iterator var6 = suffixes.iterator();

            while(true) {
                Resource[] resources;
                do {
                    do {
                        if (!var6.hasNext()) {
                            if (result.isEmpty()) {
                                logger.info(String.format("No process definitions were found for autodeployment"));
                            }

                            return result;
                        }

                        String suffix = (String)var6.next();
                        String path = prefix + suffix;
                        resources = applicationContext.getResources(path);
                    } while(resources == null);
                } while(resources.length <= 0);

                Resource[] var10 = resources;
                int var11 = resources.length;

                for(int var12 = 0; var12 < var11; ++var12) {
                    Resource resource = var10[var12];
                    result.add(resource);
                }
            }
        }
    }

首先对调用上述discoverProcessDefinitionResources方式时的参数的值进行说明

        this.processDefinitionLocationPrefix = "classpath:/processes/";
        this.processDefinitionLocationSuffixes = Arrays.asList("**.bpmn20.xml", "**.bpmn");
        private boolean checkProcessDefinitions = true;

所以也就是当checkProcessDefinitions为true时,将获取所有根目录下processes文件下后缀.bpmn20.xml和.bpmn的所有资源,否则直接返回一个空的resource集合。到这里想必已经知道怎么改了,直接设置checkProcessDefinitions为false,让它在初始化启动时不从processes文件夹下加载资源就可以了。

只需要在application.yml添加

spring:
  activiti:
    check-process-definitions: false

此时查看数据库中,activiti的表已经创建好啦!

三、总结

springboot2.x整合activiti6.0步骤

  • 第一步:加入pom依赖
        
            org.activiti
            activiti-spring-boot-starter-basic
            6.0.0
        
  • 第二步:排除org.activiti.spring.boot.SecurityAutoConfiguration的构建
    启动类注解上添加配置
@SpringBootApplication(exclude = {org.activiti.spring.boot.SecurityAutoConfiguration.class})
  • 第三步:在classpath下添加processes文件夹或者在application.yml中添加配置
spring:
  activiti:
    check-process-definitions: false

你可能感兴趣的:(activiti6.0整合springboot2.x以及bug解决)