您好,登錄后才能下訂單哦!
本篇內容主要講解“SpringBoot怎么整合Activiti7”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“SpringBoot怎么整合Activiti7”吧!
1.使用idea創建springboot項目
在工程的pom.xml文件中引入相關的依賴,具體依賴如下所示
<?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文件配置
為了能夠實現activiti7生成的表放到mysql數據庫中,需要在配置文件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.添加springsecurity安全框架整合配置
activiti7與springboot整合后,默認情況下,集成了springsecurity安全框架,這樣我們就要去準備springsecurity整合進來的相關用戶權限配置信息
1)添加securityutil類
為了能夠快速實現springsecurity安全框架的配置,所添加的一個組件。
@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'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); } }
這個類可以從activiti7官方提供的example中找到。
2)添加demoapplicationconfig類
它的作用是為了實現springsecurity框架的用戶權限的配置,這樣我們就可以在系統中使用用戶權限信息
@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)創建測試類
@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()); } }
運行結果:
遇到的問題:
1)歷史表默認不會生成,需在application.yml中配置
spring: activiti: #檢測歷史表是否存在 db-history-used: true #記錄歷史等級 可配置的歷史級別有none, activity, audit, full history-level: full
到此,相信大家對“SpringBoot怎么整合Activiti7”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。