Activiti7之整合spring和springboot 整合spring
通过 org.activiti.spring.SpringProcessEngineConfiguration 与 Spring 整合⽅式来创建ProcessEngine 对象。
1.导⼊依赖
jdk的版本是1.8
junit的版本必须是4.12以上
1<dependencies>
2<dependency>
3<groupId>org.activiti</groupId>
4<artifactId>activiti-engine</artifactId>
5<version>7.0.0.Beta1</version>
6</dependency>
7
8<dependency>
9<groupId>org.activiti</groupId>
10<artifactId>activiti-bpmn-model</artifactId>
11<version>7.0.0.Beta1</version>
12</dependency>
13
14<dependency>
15<groupId>org.activiti</groupId>
16<artifactId>activiti-bpmn-converter</artifactId>
17<version>7.0.0.Beta1</version>
18</dependency>
19
20<dependency>
21<groupId>org.activiti</groupId>
22<artifactId>activiti-json-converter</artifactId>
23<version>7.0.0.Beta1</version>
24</dependency>
25
26<!-- <dependency>
27 <groupId>org.activiti</groupId>
28 <artifactId>activiti-bpmn-layout</artifactId>
29 <version>7.0.0.Beta1</version>
30 </dependency>-->
31
32<dependency>
33<groupId>org.activiti.cloud</groupId>
34<artifactId>activiti-cloud-services-api</artifactId>
35<version>7.0.0.Beta1</version>
36</dependency>
37
38<!--activiti和Spring整合依赖-->
39<dependency>
40<groupId>org.activiti</groupId>
41<artifactId>activiti-spring</artifactId>
42<version>7.0.0.Beta1</version>
43</dependency>
44
45<!--数据库依赖-->
46<dependency>
47<groupId>mysql</groupId>
48<artifactId>mysql-connector-java</artifactId>
49<version>5.1.38</version>
50</dependency>
51
52
53<!--单测依赖-->
54<dependency>
55<groupId>junit</groupId>
56<artifactId>junit</artifactId>
57<version>4.12</version>
58</dependency>
59
60<dependency>
61<groupId>org.springframework</groupId>
62<artifactId>spring-test</artifactId>
63<version>5.0.7.RELEASE</version>
64</dependency>
65
66
67<!-- log start -->
68<dependency>
69<groupId>log4j</groupId>
70<artifactId>log4j</artifactId>
71<version>${log4j.version}</version>
72</dependency>
73<dependency>
74<groupId>org.slf4j</groupId>
75<artifactId>slf4j-api</artifactId>
76<version>${slf4j.version}</version>
77</dependency>
78<dependency>
79<groupId>org.slf4j</groupId>
80<artifactId>slf4j-log4j12</artifactId>
81<version>${slf4j.version}</version>
82</dependency>
83
84
85<dependency>
86<groupId&batis</groupId>
87<artifactId>mybatis</artifactId>
88<version>3.4.5</version>
89</dependency>
90
91
92<!--数据源-->
93<dependency>
94<groupId>commons-dbcp</groupId>
95<artifactId>commons-dbcp</artifactId>
96<version>1.4</version>
97</dependency>
98
99<!-- mvnrepository/artifact/commons-io/commons-io -->
100<dependency>
101<groupId>commons-io</groupId>
102<artifactId>commons-io</artifactId>
103<version>2.4</version>
104</dependency>
105
106</dependencies>
107<repositories>
108<repository>
109<id>alfresco</id>
110<name>Activiti Releases</name>
111<url>artifacts.alfresco/nexus/content/repositories/activiti-releases/</url>
112<releases>
113<enabled>true</enabled>
114</releases>
115</repository>
116</repositories>
依赖
2.配置Activiti
1配置内容:1.数据源 2.配置processEngine对象 3.配置XXXXService
2 <!--数据源-->
3 <bean id="dataSource" class="org.apachemons.dbcp.BasicDataSource">
4 <property name="driverClassName" value="sql.jdbc.Driver"/>
5 <property name="url" value="jdbc:mysql://localhost:3306/activiti-y2170"/>
6 <property name="username" value="root"/>
7 <property name="password" value="root"/>
8 </bean>
9
10 <!--配置ProcessEngineConfiguration-->
11 <bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">
12 <!--数据源-->
13 <property name="dataSource" ref="dataSource"/>
14 <!--配置事务-->
15 <property name="transactionManager" ref="transactionManager"/>
16 <!--数据⽣成策略true false create drop-create -->
17 <property name="databaseSchemaUpdate" value="true"/>
18 </bean>
19
20 <!--配置ProcessEngine-->
21 <bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean">
22 <property name="processEngineConfiguration" ref="processEngineConfiguration"/>
23 </bean>
24
25 <!--配置RepositoryService-->
26 <bean id="repositoryService" factory-bean="processEngine" factory-method="getRepositoryService"/>
27 <!--配置RuntimeService-->
28 <bean id="runtimeService" factory-bean="processEngine" factory-method="getRuntimeService"/>
29 <!--配置TaskService-->
30 <bean id="taskService" factory-bean="processEngine" factory-method="getTaskService"/>
31 <!--配置HistoryService-->
32 <bean id="historyService" factory-bean="processEngine" factory-method="getHistoryService"/>
33
34 <!--配置事务管理器-->
35 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
36 <property name="dataSource" ref="dataSource"/>
37 </bean>
配置
3.测试
1package com.wish;
2
spring framework版本3import static org.junit.Assert.assertTrue;
4
5import ine.RepositoryService;
6import org.junit.Test;
7import org.junit.runner.RunWith;
8import st.context.ContextConfiguration;
9import st.context.junit4.SpringJUnit4ClassRunner;
10
11import javax.annotation.Resource;
12
13/**
14 * Unit test for simple App.
15*/
16 @RunWith(SpringJUnit4ClassRunner.class) //测试⽅式 junit4.12+
17 @ContextConfiguration("l") //配置
18public class AppTest
19 {
20 @Resource
21private RepositoryService repositoryService;
22 @Test
23public void shouldAnswerWithTrue()
24 {
25 System.out.println("部署对象:"+repositoryService);
26 }
27 }
测试
运⾏结果:接下来就可以部署了
使⽤RepositoryService 进⾏部署
1/**
2 * 测试流程部署
3*/
4 @Test
5public void deployment(){
6 ateDeployment()
7 .addClasspathResource("flowchart/process.bpmn")
8 .name("测试Spring流程部署")
9 .deploy();
10 }
repositoryService部署
运⾏结果
执⾏流程分析
下⾯我们⼀起来分析Activiti 与Spring 整合加载的过程。如下图所⽰:
从图中我们可以看出,⾸先我们加载 l 配置⽂件,⽽该配置⽂件中⼜会加载SpringProcessEngineConfiguration 对象,
这个对象它需要依赖注⼊ dataSource 对象和transactionManager 对象。
其次会加载ProcessEngineFactoryBean ⼯⼚来创建ProcessEngine 对象,⽽ProcessEngineFactoryBean ⼯⼚⼜需要依赖注⼊ processEngineConfiguration 对象。最后由processEngine 对象来负责创建我们的 Service 对象,从⽽简化 Activiti 的开发过程。在程序代码中我们可以根据实际需求来决定使⽤的Service 对象。
Activiti7的新特性
Activiti7的GitHub官⽅⽹站:
下载 Activiti-Core 的Example ⽰例:
Activiti7 为了简化对⼯作流的操作,特别在原有 API 的基础上再次进⾏封闭,这样我们原来所学习的Activiti 基本API 就被封闭起来了。
具体要学习的包括:
ProcessRuntime 接⼝TaskRuntime 接⼝
ProcessRuntime 接⼝
通过上⾯的分析,我们发现使⽤ Activiti7 开发时,只要注⼊ProcessRuntime 的实现对象,就可以实现流程定义信息的操作。
当然这个过程中因为 Activiti7 与SpringSecurity 的强耦合,引导我们也必须将SpringSecurity 加⼊进来。
TaskRuntime 接⼝
上⾯部分给我们介绍了如何引⼊Activiti Core 所需的坐标,同时介绍了如何添加 TaskRuntime 实现对象,源码介绍等。我们会发现TaskRuntime 本⾝就是对于TaskService 的⼀个封装。
SpringBoot整合Activiti7
导⼊依赖
1<parent>
2<groupId>org.springframework.boot</groupId>
3<artifactId>spring-boot-starter-parent</artifactId>
4<version>2.1.4.RELEASE</version>
5</parent>
6
7
8<dependency>
9<groupId>org.springframework.boot</groupId>
10<artifactId>spring-boot-starter-jdbc</artifactId>
11</dependency>
12<dependency>
13<groupId>org.springframework.boot</groupId>
14<artifactId>spring-boot-starter-web</artifactId>
15</dependency>
16<dependency>
17<groupId>org.springframework.boot</groupId>
18<artifactId>spring-boot-starter-test</artifactId>
19<scope>test</scope>
20</dependency>
21<!-- mvnrepository/artifact/org.activiti/activiti-spring-boot-starter -->
22<dependency>
23<groupId>org.activiti</groupId>
24<artifactId>activiti-spring-boot-starter</artifactId>
25<version>7.0.0.Beta2</version>
26</dependency>
27<dependency>
28<groupId&batis</groupId>
29<artifactId>mybatis</artifactId>
30<version>3.4.5</version>
31</dependency>
32<dependency>
33<groupId>mysql</groupId>
34<artifactId>mysql-connector-java</artifactId>
35<version>5.1.38</version>
36</dependency>
依赖
配置l
1 spring:
2 datasource:
3 url: jdbc:mysql://localhost:3306/activiti-wn?useUnicode=true&characterEncoding=utf8&serverT imezone=GMT
4 username : root
5 password : 123456
6 driver-class-name: sql.jdbc.Driver
7 activiti:
8 db-history-used: true #开启历史信息
配置l
将SpringSecurity配置添加到项⽬当中
SecurityUtil关于权限登陆
1package com.wish.activiti;
2
3import org.springframework.beans.factory.annotation.Autowired;
4import org.Authentication;
5import org.GrantedAuthority;
6import org.ontext.SecurityContextHolder;
7import org.ontext.SecurityContextImpl;
8import org.userdetails.UserDetails;
9import org.userdetails.UserDetailsService;
10import org.springframework.stereotype.Component;
11
12import java.util.Collection;
13
14 @Component
15public class SecurityUtil {
16
17 @Autowired
18private UserDetailsService userDetailsService;
19
20public void logInAs(String username) {
21
22 UserDetails user = userDetailsService.loadUserByUsername(username);
23if (user == null) {
24throw new IllegalStateException("User " + username + " doesn't exist, please provide a valid user");
25 }
26
27 SecurityContextHolder.setContext(new SecurityContextImpl(new Authentication() {
28 @Override
29public Collection<? extends GrantedAuthority> getAuthorities() {
Authorities();
31 }
32
33 @Override
34public Object getCredentials() {
Password();
36 }
37
38 @Override
39public Object getDetails() {
40return user;
41 }
42
43 @Override
44public Object getPrincipal() {
45return user;
46 }
47
48 @Override
49public boolean isAuthenticated() {
50return true;
51 }
52
53 @Override
54public void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException {
55
56 }
57
58 @Override
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论