[关闭]
@ruoli 2016-08-22T09:47:11.000000Z 字数 4558 阅读 2157

Activiti集成Spring配置

Activiti


1、新建Maven-Web工程

pom.xml文件参考如下:

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>activitiSpring</groupId>
  5. <artifactId>activitiSpring</artifactId>
  6. <version>0.0.1-SNAPSHOT</version>
  7. <packaging>jar</packaging>
  8. <name>activitiSpring</name>
  9. <url>http://maven.apache.org</url>
  10. <properties>
  11. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  12. </properties>
  13. <dependencies>
  14. <dependency>
  15. <groupId>org.activiti</groupId>
  16. <artifactId>activiti-engine</artifactId>
  17. <version>5.15.1</version>
  18. </dependency>
  19. <!-- activiti 与 Spring 集成 -->
  20. <dependency>
  21. <groupId>org.activiti</groupId>
  22. <artifactId>activiti-spring</artifactId>
  23. <version>5.15.1</version>
  24. </dependency>
  25. <dependency>
  26. <groupId>junit</groupId>
  27. <artifactId>junit</artifactId>
  28. <version>4.8.1</version>
  29. </dependency>
  30. <dependency>
  31. <groupId>mysql</groupId>
  32. <artifactId>mysql-connector-java</artifactId>
  33. <version>5.1.38</version>
  34. </dependency>
  35. </dependencies>
  36. </project>

2、新建Activiti配置文件spring-context-activiti.xml

将此配置文件放置在资源根目录下,文件内容参考如下:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:context="http://www.springframework.org/schema/context"
  4. xmlns:tx="http://www.springframework.org/schema/tx"
  5. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
  8. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
  9. <bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
  10. <property name="driverClass" value="com.mysql.jdbc.Driver" />
  11. <property name="url" value="jdbc:mysql://127.0.0.1:3306/test" />
  12. <property name="username" value="root" />
  13. <property name="password" value="root" />
  14. </bean>
  15. <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  16. <property name="dataSource" ref="dataSource" />
  17. </bean>
  18. <bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">
  19. <property name="dataSource" ref="dataSource" />
  20. <property name="transactionManager" ref="transactionManager" />
  21. <property name="databaseSchemaUpdate" value="true" />
  22. <property name="mailServerHost" value="localhost" />
  23. <property name="mailServerPort" value="5025" />
  24. <property name="jpaHandleTransaction" value="true" />
  25. <property name="jpaCloseEntityManager" value="true" />
  26. <property name="jobExecutorActivate" value="false" />
  27. </bean>
  28. <bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean">
  29. <property name="processEngineConfiguration" ref="processEngineConfiguration" />
  30. </bean>
  31. <bean id="repositoryService" factory-bean="processEngine" factory-method="getRepositoryService" />
  32. <bean id="runtimeService" factory-bean="processEngine" factory-method="getRuntimeService" />
  33. <bean id="taskService" factory-bean="processEngine" factory-method="getTaskService" />
  34. <bean id="historyService" factory-bean="processEngine" factory-method="getHistoryService" />
  35. <bean id="managementService" factory-bean="processEngine" factory-method="getManagementService" />
  36. </beans>

3、编写测试类模拟Srping环境

  1. package test;
  2. import java.util.List;
  3. import org.activiti.engine.ProcessEngine;
  4. import org.activiti.engine.repository.ProcessDefinition;
  5. import org.junit.Test;
  6. import org.springframework.context.ApplicationContext;
  7. import org.springframework.context.support.FileSystemXmlApplicationContext;
  8. public class ActivitiSpringTest {
  9. @Test
  10. public void test(){
  11. ApplicationContext ac = new FileSystemXmlApplicationContext("classpath*:spring-context-activiti.xml");
  12. ProcessEngine processEngine=(ProcessEngine)ac.getBean("processEngine");
  13. List<ProcessDefinition> list = processEngine.getRepositoryService()
  14. .createProcessDefinitionQuery()
  15. .list();
  16. if(list!=null && list.size()>0){
  17. for(ProcessDefinition pd:list){
  18. System.out.println("流程定义的名称:"+pd.getName());
  19. }
  20. }
  21. }
  22. }

如能正常获取对象并输出结果,则说明环境配置正确。

4、项目集成

前面三步完成之后,需要将spring-context-activiti.xml文件在项目中进行配置和引入,在web.xml中进行如下配置:

  1. <context-param>
  2. <param-name>contextConfigLocation</param-name>
  3. <param-value>classpath*:/spring-context-activiti.xml.xml</param-value>
  4. </context-param>

此步骤依赖于项目已经有了Spring环境,将Activiti各核心对象交由Spring进行管理,上述配置完成后,即可在 Spring配置的Controller或Service中使用 @Autowired 注解注入Activiti对象。
如下:

  1. @Autowired
  2. private ProcessEngineFactoryBean processEngine;
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注