• Activiti(五)整合Spring和SpringBoot


    一:整合Spring

    1.依赖信息

     <properties>
            <slf4j.version>1.6.6</slf4j.version>
            <log4j.version>1.2.12</log4j.version>
        </properties>
        <dependencies>
        <!--activiti常规依赖-->
        <dependency>
            <groupId>org.activiti</groupId>
            <artifactId>activiti-engine</artifactId>
            <version>7.0.0.Beta1</version>
        </dependency>
    
        <dependency>
            <groupId>org.activiti</groupId>
            <artifactId>activiti-bpmn-model</artifactId>
            <version>7.0.0.Beta1</version>
        </dependency>
    
        <dependency>
            <groupId>org.activiti</groupId>
            <artifactId>activiti-bpmn-converter</artifactId>
            <version>7.0.0.Beta1</version>
        </dependency>
    
        <dependency>
            <groupId>org.activiti</groupId>
            <artifactId>activiti-json-converter</artifactId>
            <version>7.0.0.Beta1</version>
        </dependency>
    
        <dependency>
            <groupId>org.activiti</groupId>
            <artifactId>activiti-bpmn-layout</artifactId>
            <version>7.0.0.Beta1</version>
        </dependency>
    
        <dependency>
            <groupId>org.activiti.cloud</groupId>
            <artifactId>activiti-cloud-services-api</artifactId>
            <version>7.0.0.Beta1</version>
        </dependency>
    
        <!--activiti和Spring整合依赖-->
        <dependency>
            <groupId>org.activiti</groupId>
            <artifactId>activiti-spring</artifactId>
            <version>7.0.0.Beta1</version>
        </dependency>
    
        <!--数据库依赖-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.38</version>
        </dependency>
    
    
        <!--单测依赖-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
    
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.0.7.RELEASE</version>
        </dependency>
    
    
        <!-- log start -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>${log4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>${slf4j.version}</version>
        </dependency>
        <!--数据源-->
        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.4</version>
        </dependency>
    
        <!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>
        </dependencies>

    2.Spring配置文件

    activiti-spring.xml

    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans.xsd
         http://www.springframework.org/schema/tx
         http://www.springframework.org/schema/tx/spring-tx.xsd
         http://www.springframework.org/schema/aop
         http://www.springframework.org/schema/aop/spring-aop.xsd
         ">
        <!-- 数据源 -->
        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
            <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
            <property name="url" value="jdbc:mysql://localhost:3306/activiti"/>
            <property name="username" value="root"/>
            <property name="password" value="123"/>
            <property name="maxActive" value="3"/>
            <property name="maxIdle" value="1"/>
        </bean>
        <!-- 工作流引擎配置 bean -->
        <bean    id="processEngineConfiguration"
                 class="org.activiti.spring.SpringProcessEngineConfiguration">
            <!-- 数据源 -->
            <property name="dataSource" ref="dataSource"/>
            <!-- 使用 spring 事务管理器 -->
            <property name="transactionManager" ref="transactionManager"/>
            <!-- 数据库策略 -->
            <property name="databaseSchemaUpdate" value="drop-create"/>
        </bean>
    
    
    
        <!-- 流程引擎 -->
        <bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean">
            <property name="processEngineConfiguration" ref="processEngineConfiguration"/>
        </bean>
        <!-- 资源服务 service -->
        <bean id="repositoryService" factory-bean="processEngine"
              factory-method="getRepositoryService"/>
        <!-- 流程运行 service -->
        <bean id="runtimeService" factory-bean="processEngine"
              factory-method="getRuntimeService"/>
        <!-- 任务管理 service -->
        <bean id="taskService" factory-bean="processEngine"
              factory-method="getTaskService"/>
        <!-- 历史管理 service -->
        <bean id="historyService" factory-bean="processEngine"
              factory-method="getHistoryService"/>
    
        <!-- 事务管理器 -->
        <bean id="transactionManager"
              class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource"/>
        </bean>
    
    
    </beans>

    3:测试类

    import org.activiti.engine.RepositoryService;
    import org.activiti.engine.RuntimeService;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    
    import javax.annotation.Resource;
    
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = "classpath:activiti-spring.xml")
    public class TestActiviti {
        @Resource
       private RepositoryService repositoryService;
        @Resource
        private RuntimeService runtimeService;
    
        @Test
        public void test01(){
            System.out.println("部署对象:"+repositoryService);
        }
    
        @Test
        public void deployment(){
            repositoryService.createDeployment()
                    .addClasspathResource("test.bpmn")
                    .name("测试Spring流程部署")
                    .deploy();
        }
    }

    二:整合SpringBoot

    依赖

    <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.2.2.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.ty.activiti</groupId>
        <artifactId>BootActiviti</artifactId>
        <version>1.0-SNAPSHOT</version>
    
    <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>
    
        <!-- https://mvnrepository.com/artifact/org.activiti/activiti-spring-boot-starter -->
        <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.38</version>
        </dependency>
    </dependencies>

    1.1 添加 SpringSecurity 安全框架整合配置

    因为Activiti7 与SpringBoot 整合后,默认情况下,集成了 SpringSecurity 安全框架,这样我们就要去准备SpringSecurity 整合进来的相关用户权限配置信息。

    SecurityUtil
    package com.ty.util;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.security.core.Authentication;
    import org.springframework.security.core.GrantedAuthority;
    import org.springframework.security.core.context.SecurityContextHolder;
    import org.springframework.security.core.context.SecurityContextImpl;
    import org.springframework.security.core.userdetails.UserDetails;
    import org.springframework.security.core.userdetails.UserDetailsService;
    import org.springframework.stereotype.Component;
    
    import java.util.Collection;
    
    @Component
    public class SecurityUtil {
    
        private Logger logger = LoggerFactory.getLogger(SecurityUtil.class);
    
        @Autowired
        private UserDetailsService userDetailsService;
    
        public void logInAs(String username) {
    
            UserDetails user = userDetailsService.loadUserByUsername(username);
            if (user == null) {
                throw new IllegalStateException("User " + username + " doesn'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);
        }
    }
    DemoApplicationConfiguration
    package com.ty.util;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.security.core.authority.SimpleGrantedAuthority;
    import org.springframework.security.core.userdetails.User;
    import org.springframework.security.core.userdetails.UserDetailsService;
    import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
    import org.springframework.security.crypto.password.PasswordEncoder;
    import org.springframework.security.provisioning.InMemoryUserDetailsManager;
    
    import java.util.Arrays;
    import java.util.List;
    import java.util.stream.Collectors;
    
    @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();
        }
    
    }

    配置文件

    spring:
      datasource:
        url: jdbc:mysql://localhost:3306/activiti?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT
        username : root
        password : 123
        driver-class-name: com.mysql.jdbc.Driver
      activiti:
        db-history-used: true

    Controller

    package com.ty.controller;
    
    import com.ty.util.SecurityUtil;
    import org.activiti.api.process.model.ProcessDefinition;
    import org.activiti.api.process.model.ProcessInstance;
    import org.activiti.api.process.model.builders.ProcessPayloadBuilder;
    import org.activiti.api.process.runtime.ProcessRuntime;
    import org.activiti.api.runtime.shared.query.Page;
    import org.activiti.api.runtime.shared.query.Pageable;
    import org.activiti.api.task.model.Task;
    import org.activiti.api.task.model.builders.TaskPayloadBuilder;
    import org.activiti.api.task.runtime.TaskRuntime;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import javax.annotation.Resource;
    
    @RestController
    @RequestMapping("/activiti")
    public class ActivitiController {
        @Resource
        private ProcessRuntime processRuntime;
        @Resource
        private TaskRuntime taskRuntime;
        @Resource
        private SecurityUtil securityUtil;
        /**
         * 查询流程定义
         */
        @RequestMapping("/getProcess")
        public void getProcess(){
            //查询所有流程定义信息
            Page<ProcessDefinition> processDefinitionPage = processRuntime.processDefinitions(Pageable.of(0, 10));
            System.out.println("当前流程定义的数量:"+processDefinitionPage.getTotalItems());
            //获取流程信息
            for (ProcessDefinition processDefinition:processDefinitionPage.getContent()) {
                System.out.println("流程定义信息"+processDefinition);
            }
        }
    
        /**
         * 启动流程示例
         */
        @RequestMapping("/startInstance")
        public void startInstance(){
            ProcessInstance instance = processRuntime.start(ProcessPayloadBuilder.start().withProcessDefinitionKey("myProcess_1").build());
            System.out.println(instance.getId());
        }
    
        /**
         * 获取任务,拾取任务,并且执行
         */
        @RequestMapping("/getTask")
        public void getTask(){
            securityUtil.logInAs("salaboy");        //指定组内任务人
            Page<Task> tasks = taskRuntime.tasks(Pageable.of(0, 10));
            if(tasks.getTotalItems()>0){
                for (Task task:tasks.getContent()) {
                    System.out.println("任务名称:"+task.getName());
                    //拾取任务
                    taskRuntime.claim(TaskPayloadBuilder.claim().withTaskId(task.getId()).build());
                    //执行任务
                    taskRuntime.complete(TaskPayloadBuilder.complete().withTaskId(task.getId()).build());
                }
            }
        }
    }

    流程图默认扫描classpath:processes目录下的流程图

  • 相关阅读:
    ubuntu 15.04默认root用户登陆
    hive的not in
    Spark 1.4.1中Beeline使用的gc overhead limit exceeded
    Sequoiadb该如何选择合适的SQL引擎
    scala的object知识点
    scala中同步块
    英语口语练习系列-C36-城市-谈论活动-登高
    英语口语练习系列-C35-马戏-谈论语言-己亥杂诗
    英语口语练习系列-C34-儿童-谈论物品和人-武陵春
    英语口语练习系列-C33-露营-谈论日期-离思
  • 原文地址:https://www.cnblogs.com/yjc1605961523/p/12668207.html
Copyright © 2020-2023  润新知