search
HomeJavajavaTutorialHow SpringBoot integrates Activiti7

1. Use idea to create a springboot project

Introduce relevant dependencies into the pom.xml file of the project. The specific dependencies are as follows

<?xml version="1.0" encoding="utf-8"?>
<project xmlns="http://maven.apache.org/pom/4.0.0" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
     xsi:schemalocation="http://maven.apache.org/pom/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelversion>4.0.0</modelversion>
  <parent>
    <groupid>org.springframework.boot</groupid>
    <artifactid>spring-boot-starter-parent</artifactid>
    <version>2.2.1.release</version>
    <relativepath/> <!-- lookup parent from repository -->
  </parent>
  <groupid>com.example</groupid>
  <artifactid>activiti</artifactid>
  <version>0.0.1-snapshot</version>
  <name>activiti</name>
  <description>demo project for spring boot</description>

  <properties>
    <project.build.sourceencoding>utf-8</project.build.sourceencoding>
    <project.reporting.outputencoding>utf-8</project.reporting.outputencoding>
    <java.version>1.8</java.version>
  </properties>

  <dependencies>
    <dependency>
      <groupid>org.springframework.boot</groupid>
      <artifactid>spring-boot-starter-jdbc</artifactid>
    </dependency>

    <dependency>
      <groupid>org.springframework.boot</groupid>
      <artifactid>spring-boot-starter-web</artifactid>
    </dependency>

    <dependency>
      <groupid>org.springframework.boot</groupid>
      <artifactid>spring-boot-starter-test</artifactid>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupid>org.activiti</groupid>
      <artifactid>activiti-spring-boot-starter</artifactid>
      <version>7.0.0.beta2</version>
    </dependency>
    <dependency>
      <groupid>org.mybatis</groupid>
      <artifactid>mybatis</artifactid>
      <version>3.4.5</version>
    </dependency>
    <dependency>
      <groupid>mysql</groupid>
      <artifactid>mysql-connector-java</artifactid>
      <version>5.1.27</version>
    </dependency>
    <dependency>
      <groupid>org.projectlombok</groupid>
      <artifactid>lombok</artifactid>
      <optional>true</optional>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupid>org.springframework.boot</groupid>
        <artifactid>spring-boot-maven-plugin</artifactid>
      </plugin>
    </plugins>
  </build>

</project>

2.application.yml file configuration

In order to be able to put the tables generated by activiti7 into the mysql database, you need to add relevant configurations to the configuration file application.yml

server:
 port: 8085
spring:
 application:
  name: spring-activiti
 datasource:
  url: jdbc:mysql://localhost:3306/activiti?useunicode=true&characterencoding=utf8&servertimezone=gmt&nullcatalogmeanscurrent=true
  username: root
  password: 123456
  driver-class-name: com.mysql.jdbc.driver
 activiti:
  #1.flase: 默认值。activiti在启动时,会对比数据库表中保存的版本,如果没有表或者版本不匹配,将抛出异常
  #2.true: activiti会对数据库中所有表进行更新操作。如果表不存在,则自动创建
  #3.create_drop: 在activiti启动时创建表,在关闭时删除表(必须手动关闭引擎,才能删除表)
  #4.drop-create: 在activiti启动时删除原来的旧表,然后在创建新表(不需要手动关闭引擎)
  database-schema-update: true
  #检测历史表是否存在
  db-history-used: true
  #记录历史等级 可配置的历史级别有none, activity, audit, full
  history-level: full
  #校验流程文件,默认校验resources下的processes文件夹里的流程文件
  check-process-definitions: false

3. Add springsecurity security framework integration configuration

After activiti7 is integrated with springboot, the springsecurity security framework is integrated by default, so we need to prepare springsecurity Integrated related user permission configuration information

1) Add the securityutil class

A component added in order to quickly implement the configuration of the springsecurity security framework.

@component
public class securityutil {

  private logger logger = loggerfactory.getlogger(securityutil.class);

  @autowired
  @qualifier("myuserdetailsservice")
  private userdetailsservice userdetailsservice;

  public void loginas(string username) {

    userdetails user = userdetailsservice.loaduserbyusername(username);
    if (user == null) {
      throw new illegalstateexception("user " + username + " doesn&#39;t exist, please provide a valid user");
    }
    logger.info("> logged in as: " + username);
    securitycontextholder.setcontext(new securitycontextimpl(new authentication() {
      @override
      public collection<? extends grantedauthority> getauthorities() {
        return user.getauthorities();
      }

      @override
      public object getcredentials() {
        return user.getpassword();
      }

      @override
      public object getdetails() {
        return user;
      }

      @override
      public object getprincipal() {
        return user;
      }

      @override
      public boolean isauthenticated() {
        return true;
      }

      @override
      public void setauthenticated(boolean isauthenticated) throws illegalargumentexception {

      }

      @override
      public string getname() {
        return user.getusername();
      }
    }));
    org.activiti.engine.impl.identity.authentication.setauthenticateduserid(username);
  }
}

This class can be found in the example provided by activiti7.

2) Add the demoapplicationconfig class

Its function is to configure the user permissions of the springsecurity framework, so that we can use user permission information in the system

@configuration
public class demoapplicationconfiguration {

  private logger logger = loggerfactory.getlogger(demoapplicationconfiguration.class);

  @bean
  public userdetailsservice myuserdetailsservice() {

    inmemoryuserdetailsmanager inmemoryuserdetailsmanager = new inmemoryuserdetailsmanager();

    string[][] usersgroupsandroles = {
        {"salaboy", "password", "role_activiti_user", "group_activititeam"},
        {"ryandawsonuk", "password", "role_activiti_user", "group_activititeam"},
        {"erdemedeiros", "password", "role_activiti_user", "group_activititeam"},
        {"other", "password", "role_activiti_user", "group_otherteam"},
        {"system", "password", "role_activiti_user"},
        {"admin", "password", "role_activiti_admin"},
    };

    for (string[] user : usersgroupsandroles) {
      list<string> authoritiesstrings = arrays.aslist(arrays.copyofrange(user, 2, user.length));
      logger.info("> registering new user: " + user[0] + " with the following authorities[" + authoritiesstrings + "]");
      inmemoryuserdetailsmanager.createuser(new user(user[0], passwordencoder().encode(user[1]),
          authoritiesstrings.stream().map(s -> new simplegrantedauthority(s)).collect(collectors.tolist())));
    }


    return inmemoryuserdetailsmanager;
  }


  @bean
  public passwordencoder passwordencoder() {
    return new bcryptpasswordencoder();
  }

}

4) Create test class

@springboottest
@slf4j
class activitiapplicationtests {

  @autowired
  private processengine processengine;

  /**
   * 流程定义的部署
   */
  @test
  public void createdeploy() { 
    repositoryservice repositoryservice = processengine.getrepositoryservice();

    deployment deployment = repositoryservice.createdeployment()
        .addclasspathresource("diagram/holiday.bpmn")//添加bpmn资源
        .addclasspathresource("diagram/holiday.png")
        .name("请假申请单流程")
        .deploy(); 
        
    log.info("流程部署id:" + deployment.getname());
    log.info("流程部署名称:" + deployment.getid());
  }
}

Running results:

How SpringBoot integrates Activiti7

How SpringBoot integrates Activiti7

Problems encountered:

1) The history table will not be generated by default and needs to be configured in application.yml

spring:
 activiti:
  #检测历史表是否存在
  db-history-used: true
  #记录历史等级 可配置的历史级别有none, activity, audit, full
  history-level: full

The above is the detailed content of How SpringBoot integrates Activiti7. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:亿速云. If there is any infringement, please contact admin@php.cn delete
How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?Mar 17, 2025 pm 05:46 PM

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?Mar 17, 2025 pm 05:45 PM

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?Mar 17, 2025 pm 05:44 PM

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?Mar 17, 2025 pm 05:43 PM

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

How does Java's classloading mechanism work, including different classloaders and their delegation models?How does Java's classloading mechanism work, including different classloaders and their delegation models?Mar 17, 2025 pm 05:35 PM

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.