接管SpringBoot对Activiti的数据源自动配置

SpringBoot的自动配置真的让人又爱又恨,但还是爱更多一点。

SpringBoot想要帮我们自动配置好一切,但是有时候配置的却并不是我们需要的,甚至有时候会默默的坑我们。

我的项目是一个多数据源的项目(多数据源戳这里),其中有一个主数据源configDataSource,用来存放配置的库,我又新建了一个activitiDataSource,想要存放activiti的工作表。但是springboot却把configDataSource配置为了Activiti的库。

不想把activiti的自动配置关掉,毕竟用起来还是很方便的。那么只能更改springboot对activiti的默认配置了,经过实践可以使用如下方式:注释解释的很清楚了,就不啰嗦了。

/**
 * @author kingboy--KingBoyWorld@163.com
 * @date 2017/8/30 上午12:54
 * @desc Activiti的数据和事物配置.
 */
@Configuration//声名为配置类,继承Activiti抽象配置类
public class ActivitiConfig extends AbstractProcessEngineAutoConfiguration {

    @Resource
    DataSource activitiDataSource;//注入配置好的数据源

    @Resource
    PlatformTransactionManager activitiTransactionManager;//注入配置好的事物管理器

    //注入数据源和事务管理器
    @Bean
    public SpringProcessEngineConfiguration springProcessEngineConfiguration(
        SpringAsyncExecutor springAsyncExecutor) throws IOException {
        return this.baseSpringProcessEngineConfiguration(activitiDataSource, activitiTransactionManager, springAsyncExecutor);
    }
}

到这里就搞定了,springboot就会使用我们声名的数据源来配置Activiti了。

下面附上我的数据源和事物管理器的配置。

配置文件:

#-----------------------------------数据源配置-----------------------------
#activiti数据源
spring.datasource.activiti.url=jdbc:mysql://localhost:3306/activiti?characterEncoding=utf8&serverTimezone=Asia/Shanghai&useSSL=false
spring.datasource.activiti.username=root
spring.datasource.activiti.password=123456
spring.datasource.activiti.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.activiti.maxActive=20
spring.datasource.activiti.initialSize=5
spring.datasource.activiti.minIdle=5
spring.datasource.activiti.maxWait=60000
spring.datasource.activiti.maxPoolPreparedStatementPerConnectionSize=20
spring.datasource.activiti.timeBetweenEvictionRunsMillis=60000
spring.datasource.activiti.minEvictableIdleTimeMillis=300000
spring.datasource.activiti.poolPreparedStatements=true

配置类

1.属性读取 ActivitiDataSourceProperties.java

package com.kingboy.activiti.config;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * @author kingboy
 * @date 2017/6/19 下午5:15
 * @desc configuration的数据池属性
 */
@ConfigurationProperties(prefix = "spring.datasource.activiti")
@Data
@Component
public class ActivitiDataSourceProperties {

    private String url;

    private String username;

    private String password;

    private String driverClassName;

    private Integer maxActive;

    private Integer initialSize;

    private Integer minIdle;

    private Integer maxWait;

    private Integer maxPoolPreparedStatementPerConnectionSize;

    private Integer timeBetweenEvictionRunsMillis;

    private Integer minEvictableIdleTimeMillis;

    private Boolean poolPreparedStatements;

}
  1. 数据源和事物配置ActivitiJPAConfig.java
/**
 * @author kingboy
 * @date 2017/6/18 下午11:13
 * @desc 数据源配置
 */
@Configuration
@EnableTransactionManagement//开启事物管理
@EnableJpaRepositories(//自定义数据管理的配置
        //指定EntityManager的创建工厂Bean
        entityManagerFactoryRef = "activitiEntityManagerFactory",
        //指定事物管理的Bean
        transactionManagerRef = "activitiTransactionManager",
        //指定管理的实体位置
        basePackages = {"com.kingboy.activiti.domain"})
public class ActivitiJPAConfig {

    /*
     * 配置数据源
     */
    @Bean
    public DataSource activitiDataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        //设置数据源的属性
        setDruidProperties(dataSource);
        return dataSource;
    }


    //注入数据源配置信息
    @Autowired
    ActivitiDataSourceProperties config;

    //设置数据源的属性的方法
    private void setDruidProperties(DruidDataSource dataSource) {
        dataSource.setUrl(config.getUrl());
        dataSource.setUsername(config.getUsername());
        dataSource.setPassword(config.getPassword());
        dataSource.setDriverClassName(config.getDriverClassName());
        dataSource.setMaxActive(config.getMaxActive());
        dataSource.setInitialSize(config.getInitialSize());
        dataSource.setMinIdle(config.getMinIdle());
        dataSource.setMaxWait(config.getMaxWait());
        dataSource.setMaxPoolPreparedStatementPerConnectionSize(config.getMaxPoolPreparedStatementPerConnectionSize());
        dataSource.setTimeBetweenEvictionRunsMillis(config.getTimeBetweenEvictionRunsMillis());
        dataSource.setMinEvictableIdleTimeMillis(config.getMinEvictableIdleTimeMillis());
        dataSource.setPoolPreparedStatements(config.getPoolPreparedStatements());
    }

    /*
     * 配置实体管理工厂Bean
     */
    @Bean
    public LocalContainerEntityManagerFactoryBean activitiEntityManagerFactory(EntityManagerFactoryBuilder builder) {
        return builder
                .dataSource(activitiDataSource())
                .packages("com.kingboy.activiti.domain")//设置实体类所在位置
                .persistenceUnit("activiti")
                .properties(getProperties(activitiDataSource()))//设置hibernate通用配置
                .build();
    }

    //注入spring自带的jpa属性类
    @Autowired
    private JpaProperties jpaProperties;

    /*
     *拿到hibernate的通用配置
     */
    private Map<String, String> getProperties(DataSource dataSource) {
        return jpaProperties.getHibernateProperties(dataSource);
    }

    /*
     *配置事物管理的Bean
     */
    @Bean
    public PlatformTransactionManager activitiTransactionManager(EntityManagerFactoryBuilder builder) {
        return new JpaTransactionManager(activitiEntityManagerFactory(builder).getObject());
    }


}
原文地址:https://blog.csdn.net/KingBoyWorld/article/details/77703565
posted @ 2019-06-25 17:51  星朝  阅读(2861)  评论(0编辑  收藏  举报