首页 > 其他分享 >SpringBoot集成Activiti7-单独配置数据源

SpringBoot集成Activiti7-单独配置数据源

时间:2023-04-02 20:22:06浏览次数:46  
标签:SpringBoot activiti 数据源 bean Activiti7 conf new public activitiProperties

框架:SpringBoot + Mybatis + Activiti7

思路:

单独给mybatis和activiti配置datasource

Mybati配置单数据源方法

单数据源只需要在yml中配置

url: jdbc:mysql://localhost:3306/localtest?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useSSL=false&nullCatalogMeansCurrent=true
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: 123456

Mybati配置双数据源方法

给每个mybatis配置类分别设置数据源,然后使用MapperScan声明作用的mapper路径

此时url需要写成jdbc-url,不然会报错,欲知为何,待我看懂ConfigurationProperties咋起作用的
// 声明businessDataSource只作用于mapper路径下
@Configuration
@MapperScan(basePackages = "com.rsb.activitispringboot.mapper", sqlSessionFactoryRef = "businessSqlSessionFactory")
public class MybatisConfig {

    @Bean(name = "businessDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.master")
    public DataSource businessDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "businessSqlSessionFactory")
    public SqlSessionFactory bizSqlSessionFactory(@Qualifier("businessDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/*.xml"));
        return bean.getObject();
    }
    @Bean(name = "businessSqlSessionTemplate")
    public SqlSessionTemplate bizSqlSessionTemplate(@Qualifier("businessSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}

activiti和mybaits分别配置数据源

activiti自己继承了mybaits,其mapper没有暴露,所以不能使用以上方法配置。

ativiti7有一个配置数据源的类ProcessEngineAutoConfiguration

@Bean
@ConditionalOnMissingBean // 不存在SpringProcessEngineConfiguration的bean的时候,才会注册这个bean
public SpringProcessEngineConfiguration springProcessEngineConfiguration(
           DataSource dataSource,
           PlatformTransactionManager transactionManager,
           SpringAsyncExecutor springAsyncExecutor,
           ActivitiProperties activitiProperties,
           ProcessDefinitionResourceFinder processDefinitionResourceFinder,
           @Autowired(required = false) List\<ProcessEngineConfigurationConfigurer> processEngineConfigurationConfigurers,
           @Autowired(required = false) List\<ProcessEngineConfigurator> processEngineConfigurators) throws IOException {
       conf.setDataSource(dataSource);
//...}

也就是说同时存在mybaits的BusinessDatasource和ActivitiDatSource,而IOC容器单例模式不能有两个相同的bean,除非name不同,所以就有2种思路。

  • mybatis配置别名,activiti不配置
  • activiti配置别名,mybaits配置/不配置

1) mybatis配置别名,activiti不配置,这种方式简单,不用实现activiti的复杂编码。优先加载activiti的数据源

@Primary其作用与功能,当有多个相同类型的bean时,使用@Primary来赋予bean更高的优先级

@Configuration
@MapperScan(basePackages = "com.rsb.activitispringboot.mapper", sqlSessionFactoryRef = "businessSqlSessionFactory")
public class MybatisConfig {
    @Bean(name = "businessDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.master")
    public DataSource businessDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "businessSqlSessionFactory")
    public SqlSessionFactory bizSqlSessionFactory(@Qualifier("businessDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/*.xml"));
        return bean.getObject();
    }

    @Bean(name = "businessSqlSessionTemplate")
    public SqlSessionTemplate bizSqlSessionTemplate(@Qualifier("businessSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}

@Configuration
public class ActivitiConfig{
    @Bean
    @Primary
    @ConfigurationProperties(prefix = "spring.activiti.datasource")
    public DataSource activitiDataSource() {
        return DataSourceBuilder.create().build();
    }
}

2) activiti配置别名,mybaits不配置 / 不配置

mybatis配置同上,可以使用Qualifier 也可以使用@Primary,建议@Qualifier,清晰明了

@Configuration
public class ActivitiConfig extends AbstractProcessEngineAutoConfiguration {

   @Bean("activitiDataSource")
   @ConfigurationProperties(prefix = "spring.activiti.datasource")
   public DataSource activitiDataSource() {
       return DataSourceBuilder.create().build();
   }

   @Bean("activitiTransactionManager")
   public PlatformTransactionManager txManager(@Qualifier("activitiDataSource") DataSource dataSource) {
       return new DataSourceTransactionManager(dataSource);
   }

   @Bean
   public SpringProcessEngineConfiguration springProcessEngineConfiguration(
           @Qualifier("activitiDataSource") DataSource dataSource,
           @Qualifier("activitiTransactionManager") PlatformTransactionManager transactionManager,
           SpringAsyncExecutor springAsyncExecutor,
           ActivitiProperties activitiProperties,
           ProcessDefinitionResourceFinder processDefinitionResourceFinder,
           @Autowired(required = false) List\<ProcessEngineConfigurationConfigurer> processEngineConfigurationConfigurers,
           @Autowired(required = false) List\<ProcessEngineConfigurator> processEngineConfigurators) 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());
       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 (processEngineConfigurationConfigurers != null) {
           for (ProcessEngineConfigurationConfigurer processEngineConfigurationConfigurer : processEngineConfigurationConfigurers) {
               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]));
       }
   }
}

标签:SpringBoot,activiti,数据源,bean,Activiti7,conf,new,public,activitiProperties
From: https://www.cnblogs.com/lordMaster/p/17250365.html

相关文章

  • SpringBoot 服务接口限流-AOP令牌桶
    前言在开发高并发系统时有三把利器用来保护系统:缓存、降级和限流。限流可以认为服务降级的一种,限流通过限制请求的流量以达到保护系统的目的。一般来说,系统的吞吐量是可以计算出一个阈值的,为了保证系统的稳定运行,一旦达到这个阈值,就需要限制流量并采取一些措施以完成限制流量的目的......
  • Springboot 系列 (27) - Springboot+HBase 大数据存储(五)| HBase REST 服务
    REST(RepresentationalStateTransfer)即表述性状态传递,是RoyFielding博士2000年在他的博士论文中提出来的一种软件架构风格。它是一种针对网络应用的设计和开发方式,可以降低开发的复杂性,提高系统的可伸缩性。在三种主流的Web服务实现方案中,与复杂的SOAP和XML-RPC相......
  • springboot集成mybatis-plus
    springboot项目先导入相关依赖mybatis-plus相关依赖<dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.0.5</version></dependency><dependency><gr......
  • SSM使用Druid数据源出现循环报错
    解决方法在数据库连接配置db.properties文件中的属性前增加前缀xml配置<beanid="dataSource"class="com.alibaba.druid.pool.DruidDataSource"><propertyname="username"value="${jdbc.username}"/>......
  • springboot起步依赖
    <parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.0.RELEASE</version><relativePath/><!--lookupparentfromrepository--></......
  • SpringBoot 热部署
    在开发过程中,通常会对业务代码不断地修改测试,在修改之后往往需要重启服务,有些服务需要加载很久才能启动成功,这种重复操作极大的降低了程序开发效率。SpringBoot框架专门提供了进行热部署的依赖启动器spring-boot-devtools,用于进行项目热部署,而无需手动重启项目。具体实现步......
  • springboot学习笔记1-基本介绍
    springboot基本介绍官方文档什么是springbootspringboot可以轻松创建独立的、生产级的基于spring的应用程序springboot直接嵌入tomcat、jetty、或undertow,无须打war包,可以直接运行springboot应用程序quickstartforspringbootdemo需求说明:quickstart创建......
  • SpringBoot的@Async注释的用法并例子
    在SpringBoot中,@Async注解用于将一个方法标记为异步执行的方法。使用该注解的方法将在一个单独的线程中异步执行,而不会阻塞调用线程。这对于处理需要长时间运行的任务或需要异步处理的任务非常有用。下面是一个简单的示例:@ServicepublicclassMyService{@Asyncpu......
  • springboot-监听器
    监听器ApplicationListener可以实现这个接口时传入对应的监听器,用于监听该事件比如:实现ApplicationListener<ContextRefreshedEvent>接口,重写onApplicationEvent方法,将ContextRefreshedEvent对象传进去。如果我们想在加载或刷新应用上下文时,也重新刷新下我们预加载的资源......
  • SpringBoot中Mybatis的应用
    创建一个SpringBoot项目,然后如下操作:(1)添加Lombok插件--简化实体类 (2)添加Mybatis框架和MySQL驱动---访问Mysql和使用Mybatis (3)创建pojo包和实体类在项目中创建pojo包,并在其中创建实体类。实体类上可以使用Lombok注解。首次使用时,需要在Ieda中安装该插件。L......