Springboot2.1.x配置Activiti7单独数据源问题
springboot2.1.x配置activiti7单独数据源问题
简介
最近基于最新的activiti7
配置了springboot2
。
简单上手使用了一番。发现市面上解决activiti7
的教程很少,采坑也比较多,在activiti6
配置数据源和activiti7
有所区别,基于activiti6
在activiti7
里是无法正常使用的。接下来让我们看下区别。
问题
activiti6
多数据源配置
6的配置比较简单点。
- 先加入配置:
# 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
用
@configurationproperties
加载以spring.datasource.activiti
开头的datasource
。创建
activiticonfig
继承abstractprocessengineautoconfiguration
方法注入springprocessengineconfiguration
bean,调用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
多数据源配置
配置还是要上面的。
创建
activiticonfig
继承abstractprocessengineautoconfiguration
方法注入springprocessengineconfiguration
bean。用
@configurationproperties
加载以spring.datasource.activiti
开头的datasource
@configuration public class activiticonfig extends abstractprocessengineautoconfiguration { @bean @configurationproperties(prefix = "spring.datasource.activiti") public datasource activitidatasource() { return datasourcebuilder.create().build(); } ...略 }
-
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 欢迎关注
推荐阅读
-
解决Druid动态数据源配置重复刷错误日志的问题
-
使用SpringBoot 配置Oracle和H2双数据源及问题
-
解决springboot利用ConfigurationProperties注解配置数据源无法读取配置信息问题
-
使用SpringBoot 配置Oracle和H2双数据源及问题
-
springmvc整合mybaits配置数据源问题
-
springmvc整合mybaits配置数据源问题
-
Springboot2.1.x配置Activiti7单独数据源问题
-
解决Druid动态数据源配置重复刷错误日志的问题
-
解决springboot利用ConfigurationProperties注解配置数据源无法读取配置信息问题