欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Springboot2.1.x配置Activiti7单独数据源问题

程序员文章站 2024-01-16 16:27:29
Springboot2.1.x配置Activiti7单独数据源问题 简介 最近基于最新的 配置了 。 简单上手使用了一番。发现市面上解决 的教程很少,采坑也比较多,在 配置数据源和 有所区别,基于 在`Activiti7`里是无法正常使用的。接下来让我们看下区别。 问题 多数据源配置 6的配置比较简 ......

springboot2.1.x配置activiti7单独数据源问题

简介

最近基于最新的activiti7配置了springboot2
简单上手使用了一番。发现市面上解决activiti7的教程很少,采坑也比较多,在activiti6配置数据源和activiti7有所区别,基于activiti6activiti7里是无法正常使用的。接下来让我们看下区别。

问题

activiti6多数据源配置

6的配置比较简单点。

  1. 先加入配置:
# activiti 数据源
spring.datasource.activiti.driver=com.mysql.jdbc.driver
spring.datasource.activiti.url=jdbc:mysql://10.1.1.97:3311/test-activiti7-db?useunicode=true&characterencoding=utf8&usessl=false&allowmultiqueries=true
spring.datasource.activiti.username=root
spring.datasource.activiti.password=rtqw123opnmer
spring.datasource.activiti.driverclassname=com.mysql.jdbc.driver
  1. @configurationproperties加载以spring.datasource.activiti开头的datasource

  2. 创建activiticonfig继承abstractprocessengineautoconfiguration方法注入springprocessengineconfigurationbean,调用abstractprocessengineautoconfiguration#basespringprocessengineconfiguration方法把创建的数据源注入。

@configuration
public class activiticonfig extends abstractprocessengineautoconfiguration {

  @bean
  @configurationproperties(prefix = "spring.datasource.activiti")
  public datasource activitidatasource() {
    return datasourcebuilder.create().build();
  }

  @bean
  public springprocessengineconfiguration springprocessengineconfiguration(
      platformtransactionmanager transactionmanager,
      springasyncexecutor springasyncexecutor) throws ioexception {

    return basespringprocessengineconfiguration(
        activitidatasource(),
        transactionmanager,
        springasyncexecutor);
  }
}

activiti6的数据源已经配置完成,如果是7的话会发现abstractprocessengineautoconfiguration#basespringprocessengineconfiguration方法已经不存在了,我们需要产考源码构建方式改造一番。

修复

activiti7多数据源配置

配置还是要上面的。

  1. 创建activiticonfig继承abstractprocessengineautoconfiguration方法注入springprocessengineconfigurationbean。

  2. @configurationproperties加载以spring.datasource.activiti开头的datasource

@configuration
public class activiticonfig extends abstractprocessengineautoconfiguration {
  
  @bean
  @configurationproperties(prefix = "spring.datasource.activiti")
  public datasource activitidatasource() {
    return datasourcebuilder.create().build();
  }
  
  ...略
}
  1. springprocessengineconfiguration注入方式改为下面:
  @bean
  @primary
  public springprocessengineconfiguration springprocessengineconfiguration(
      platformtransactionmanager transactionmanager,
      springasyncexecutor springasyncexecutor,
      activitiproperties activitiproperties,
      processdefinitionresourcefinder processdefinitionresourcefinder,
      @autowired(required = false) defaultactivitybehaviorfactorymappingconfigurer processengineconfigurationconfigurer,
      @autowired(required = false) list<processengineconfigurator> processengineconfigurators,
      usergroupmanager usergroupmanager,
      datasource datasource) throws ioexception {

    springprocessengineconfiguration conf = new springprocessengineconfiguration();
    conf.setconfigurators(processengineconfigurators);
    configureprocessdefinitionresources(processdefinitionresourcefinder,
        conf);
    conf.setdatasource(datasource);
    conf.settransactionmanager(transactionmanager);

    if (springasyncexecutor != null) {
      conf.setasyncexecutor(springasyncexecutor);
    }
    conf.setdeploymentname(activitiproperties.getdeploymentname());
    conf.setdatabaseschema(activitiproperties.getdatabaseschema());
    conf.setdatabaseschemaupdate(activitiproperties.getdatabaseschemaupdate());
    conf.setdbhistoryused(activitiproperties.isdbhistoryused());
    conf.setasyncexecutoractivate(activitiproperties.isasyncexecutoractivate());
    if (!activitiproperties.isasyncexecutoractivate()) {
      validatorset springbootstartervalidatorset = new validatorset("activiti-spring-boot-starter");
      springbootstartervalidatorset.addvalidator(new asyncpropertyvalidator());
      if (conf.getprocessvalidator() == null) {
        processvalidatorimpl processvalidator = new processvalidatorimpl();
        processvalidator.addvalidatorset(springbootstartervalidatorset);
        conf.setprocessvalidator(processvalidator);
      } else {
        conf.getprocessvalidator().getvalidatorsets().add(springbootstartervalidatorset);
      }
    }
    conf.setmailserverhost(activitiproperties.getmailserverhost());
    conf.setmailserverport(activitiproperties.getmailserverport());
    conf.setmailserverusername(activitiproperties.getmailserverusername());
    conf.setmailserverpassword(activitiproperties.getmailserverpassword());
    conf.setmailserverdefaultfrom(activitiproperties.getmailserverdefaultfrom());
    conf.setmailserverusessl(activitiproperties.ismailserverusessl());
    conf.setmailserverusetls(activitiproperties.ismailserverusetls());

    if (usergroupmanager != null) {
      conf.setusergroupmanager(usergroupmanager);
    }

    conf.sethistorylevel(activitiproperties.gethistorylevel());
    conf.setcopyvariablestolocalfortasks(activitiproperties.iscopyvariablestolocalfortasks());
    conf.setserializepojosinvariablestojson(activitiproperties.isserializepojosinvariablestojson());
    conf.setjavaclassfieldforjackson(activitiproperties.getjavaclassfieldforjackson());

    if (activitiproperties.getcustommybatismappers() != null) {
      conf.setcustommybatismappers(
          getcustommybatismapperclasses(activitiproperties.getcustommybatismappers()));
    }

    if (activitiproperties.getcustommybatisxmlmappers() != null) {
      conf.setcustommybatisxmlmappers(
          new hashset<>(activitiproperties.getcustommybatisxmlmappers()));
    }

    if (activitiproperties.getcustommybatisxmlmappers() != null) {
      conf.setcustommybatisxmlmappers(
          new hashset<>(activitiproperties.getcustommybatisxmlmappers()));
    }

    if (activitiproperties.isusestronguuids()) {
      conf.setidgenerator(new stronguuidgenerator());
    }

    if (activitiproperties.getdeploymentmode() != null) {
      conf.setdeploymentmode(activitiproperties.getdeploymentmode());
    }

    conf.setactivitybehaviorfactory(new defaultactivitybehaviorfactory());

    if (processengineconfigurationconfigurer != null) {
      processengineconfigurationconfigurer.configure(conf);
    }

    return conf;
  }

  private void configureprocessdefinitionresources(
      processdefinitionresourcefinder processdefinitionresourcefinder,
      springprocessengineconfiguration conf) throws ioexception {
    list<resource> procdefresources = processdefinitionresourcefinder
        .discoverprocessdefinitionresources();
    if (!procdefresources.isempty()) {
      conf.setdeploymentresources(procdefresources.toarray(new resource[0]));
    }
  }

常见错误

在以上配置中可能会有jdbcurl is required with driverclassname错误

解决办法如下:

# activiti 数据源
spring.datasource.activiti.driver=com.mysql.jdbc.driver
spring.datasource.activiti.url=jdbc:mysql://10.1.1.97:3311/test-activiti7-db?useunicode=true&characterencoding=utf8&usessl=false&allowmultiqueries=true
# url换为jdbc-url 解决jdbcurl is required with driverclassname错误
# 官方文档的解释是:
# 因为连接池的实际类型没有被公开,所以在您的自定义数据源的元数据中没有生成密钥,而且在ide中没有完成(因为datasource接口没有暴露属性)。
# 另外,如果您碰巧在类路径上有hikari,那么这个基本设置就不起作用了,因为hikari没有url属性(但是确实有一个jdbcurl属性)。在这种情况下,您必须重写您的配置如下:
spring.datasource.activiti.jdbc-url=${spring.datasource.activiti.url}
spring.datasource.activiti.username=root
spring.datasource.activiti.password=rtqw123opnmer
spring.datasource.activiti.driverclassname=com.mysql.jdbc.driver

总结

activiti7国内的教程不是很多,需要自己在社区里或者官方文档,源码去看了解,细心学习。

示例代码地址:

作者github:
purgeyao 欢迎关注