Activiti集成到spring中配置

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Jessechanrui/article/details/52191129

导入activiti相应的jar包
使用用maven,在pox.xml中配置

<dependency>
        	<groupId>org.activiti</groupId>
        	<artifactId>activiti-engine</artifactId>
        	<version>5.21.0</version>
        </dependency>
        <dependency>
        	<groupId>org.activiti</groupId>
        	<artifactId>activiti-spring</artifactId>
        	<version>5.21.0</version>
        </dependency>
        <dependency>
        	<groupId>org.activiti</groupId>
        	<artifactId>activiti-rest</artifactId>
        	<version>5.21.0</version>
        </dependency>

在spring.xml中配置

    <!--  Activiti配置 Start -->
    <!-- 单例json对象 -->
    <bean id="objectMapper" class="com.fasterxml.jackson.databind.ObjectMapper"/>
	
	<!-- 自定义用户模块 -->
	<bean id="customUserEntityManager" class="cn.com.agree.aweb.springmvc.activiti.CustomUserEntityManager">
	</bean>
	<bean id="customGroupEntityManager" class="cn.com.agree.aweb.springmvc.activiti.CustomGroupEntityManager">
	</bean>
	
    <!-- Activiti引擎配置 -->
	<bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">
		<property name="processEngineName" value="spring"></property>
		<property name="dataSource" ref="jndiDataSource" />
		<property name="transactionManager" ref="transactionManager" />
		<property name="databaseSchemaUpdate" value="true" />
		<property name="jobExecutorActivate" value="false" />
		<!-- web服务重启时自动部署流程文件,自动部署引擎数据库中不存在或修改过的流程定义 -->  
        <!-- <property name="deploymentResources">  
           <list>  
               <value>bpmn/*.bpmn</value>  
           </list>  
        </property>   -->
	     <!-- 自定义用户模块 -->
		<property name="customSessionFactories">
	         <list>
	              <bean class="cn.com.agree.aweb.springmvc.activiti.CustomUserEntityManagerFactory"></bean>
	              <bean class="cn.com.agree.aweb.springmvc.activiti.CustomGroupEntityManagerFactory"></bean>
	         </list>
		</property> 
	</bean>

	<!-- Activiti引擎工厂 -->
	<bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean">
		<property name="processEngineConfiguration" ref="processEngineConfiguration" />
	</bean>

	<!-- Activiti的7个Service -->
	<bean id="repositoryService" factory-bean="processEngine" factory-method="getRepositoryService" />
	<bean id="runtimeService" factory-bean="processEngine" factory-method="getRuntimeService" />
	<bean id="formService" factory-bean="processEngine" factory-method="getFormService" />
	<bean id="identityService" factory-bean="processEngine" factory-method="getIdentityService" />
	<bean id="taskService" factory-bean="processEngine" factory-method="getTaskService" />
	<bean id="historyService" factory-bean="processEngine" factory-method="getHistoryService" />
	<bean id="managementService" factory-bean="processEngine" factory-method="getManagementService" />
	
	 <!-- 集成REST服务需要的bean -->
    <bean id="restResponseFactory" class="org.activiti.rest.service.api.RestResponseFactory" />
    <bean id="contentTypeResolver" class="org.activiti.rest.common.application.DefaultContentTypeResolver" />
    
    <!--  Activiti配置 End -->

使用7个service,实现工作流的全部操作。
数据源使用项目已配置的数据源。
自定义用户模块(使用项目中的用户管理模块,放弃使用activiti的用户表),重写相应的类,对应配置中customSessionFactories的属性值,示例用户自定义如下:

package cn.com.agree.aweb.springmvc.activiti;

import org.activiti.engine.impl.interceptor.Session;
import org.activiti.engine.impl.interceptor.SessionFactory;
import org.activiti.engine.impl.persistence.entity.UserIdentityManager;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @Description: 自定义用户
 * @author chenrui  chenrui@agree.com.cn
 * @date 2016年8月2日
 *
 */
public class CustomUserEntityManagerFactory implements SessionFactory{
	 
	@Autowired
	private CustomUserEntityManager customUserEntityManager;
	
	@Override
	public Class<?> getSessionType() {
		return UserIdentityManager.class;
	}

	@Override
	public Session openSession() {
		return  customUserEntityManager;
	}



}

该类继承UserEntityManager,重写对应方法

package cn.com.agree.aweb.springmvc.activiti;

import java.util.List;
import java.util.Map;

import org.activiti.engine.identity.Group;
import org.activiti.engine.identity.Picture;
import org.activiti.engine.identity.User;
import org.activiti.engine.identity.UserQuery;
import org.activiti.engine.impl.Page;
import org.activiti.engine.impl.UserQueryImpl;
import org.activiti.engine.impl.persistence.entity.IdentityInfoEntity;
import org.activiti.engine.impl.persistence.entity.UserEntity;
import org.activiti.engine.impl.persistence.entity.UserEntityManager;
import org.springframework.beans.factory.annotation.Autowired;

import cn.com.agree.aweb.exception.DBSupportException;
import cn.com.agree.aweb.hibernate.IDbSupport;
import cn.com.agree.aweb.hibernate.entity.UserVO;

/**
 * @Description: 自定义用户管理
 * @author chenrui  chenrui@agree.com.cn
 * @date 2016年8月2日
 *
 */
public class CustomUserEntityManager extends UserEntityManager{
	@Autowired
	private IDbSupport dbSupport;

    @Override
	public User findUserById(String userId) {
		User userEntity = new UserEntity();
		try {
			UserVO userVO = queryUserByUsername(userId);
			userEntity.setId(userVO.getUserId());
			userEntity.setFirstName(userVO.getNickname());
			userEntity.setLastName(userVO.getNickname());
			userEntity.setPassword(userVO.getPassword());
			userEntity.setEmail(userVO.getMailbox());
		} catch (DBSupportException e) {
			e.printStackTrace();
		}
		return userEntity;
	}
	
	
	
	@Override
	public User createNewUser(String userId) {
		// TODO Auto-generated method stub
		User user = new UserEntity();
		return user;
	}




	@Override
	public void insertUser(User user) {
		// TODO Auto-generated method stub
		super.insertUser(user);
	}




	@Override
	public void updateUser(User updatedUser) {
		// TODO Auto-generated method stub
		super.updateUser(updatedUser);
	}

	@Override
	public void deleteUser(String userId) {
		// TODO Auto-generated method stub
		super.deleteUser(userId);
	}




	@Override
	public List<User> findUserByQueryCriteria(UserQueryImpl query, Page page) {
		// TODO Auto-generated method stub
		return super.findUserByQueryCriteria(query, page);
	}




	@Override
	public long findUserCountByQueryCriteria(UserQueryImpl query) {
		// TODO Auto-generated method stub
		return super.findUserCountByQueryCriteria(query);
	}




	@Override
	public List<Group> findGroupsByUser(String userId) {
		// TODO Auto-generated method stub
		return super.findGroupsByUser(userId);
	}




	@Override
	public UserQuery createNewUserQuery() {
		// TODO Auto-generated method stub
		return super.createNewUserQuery();
	}




	@Override
	public IdentityInfoEntity findUserInfoByUserIdAndKey(String userId,
			String key) {
		// TODO Auto-generated method stub
		return super.findUserInfoByUserIdAndKey(userId, key);
	}




	@Override
	public List<String> findUserInfoKeysByUserIdAndType(String userId,
			String type) {
		// TODO Auto-generated method stub
		return super.findUserInfoKeysByUserIdAndType(userId, type);
	}




	@Override
	public Boolean checkPassword(String userId, String password) {
		// TODO Auto-generated method stub
		return super.checkPassword(userId, password);
	}




	@Override
	public List<User> findPotentialStarterUsers(String proceDefId) {
		// TODO Auto-generated method stub
		return super.findPotentialStarterUsers(proceDefId);
	}




	@Override
	public List<User> findUsersByNativeQuery(Map<String, Object> parameterMap,
			int firstResult, int maxResults) {
		// TODO Auto-generated method stub
		return super.findUsersByNativeQuery(parameterMap, firstResult, maxResults);
	}




	@Override
	public long findUserCountByNativeQuery(Map<String, Object> parameterMap) {
		// TODO Auto-generated method stub
		return super.findUserCountByNativeQuery(parameterMap);
	}




	@Override
	public boolean isNewUser(User user) {
		// TODO Auto-generated method stub
		return super.isNewUser(user);
	}




	@Override
	public Picture getUserPicture(String userId) {
		// TODO Auto-generated method stub
		return super.getUserPicture(userId);
	}




	@Override
	public void setUserPicture(String userId, Picture picture) {
		// TODO Auto-generated method stub
		super.setUserPicture(userId, picture);
	}




	/**
	 * @param username
	 * @return
	 * @throws DBSupportException
	 */
	@SuppressWarnings("unchecked")
	public UserVO queryUserByUsername(String username) throws DBSupportException {
        List<UserVO> userList = (List<UserVO>) dbSupport.queryDataByClass(UserVO.class, 
                new String[] {"username"}, 
                new String[] {username});

        if (userList.size() == 1) {
            return userList.get(0);
        } 

        return null;
    }
	
	
}

发布REST API

在web.xml中配置

    <!-- Rest接口 Start-->
    <servlet>
	    <servlet-name>RestServlet</servlet-name>
	    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
	    <init-param>
	        <param-name>contextConfigLocation</param-name>
	        <param-value>WEB-INF/classes/spring-mvc-rest.xml</param-value>
	    </init-param>
	    <load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
	    <servlet-name>RestServlet</servlet-name>
	    <url-pattern>/rest/*</url-pattern>
	</servlet-mapping>
	<!-- Rest接口 end -->

spring-mvc-rest.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">

	<!-- 自动扫描且只扫描@Controller -->
    <context:component-scan base-package="org.activiti.rest">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
    </context:component-scan>

    <mvc:annotation-driven />
</beans>

浏览器访问:http://localhost:8080/projectName/rest/management/properties即可访问,返回json格式的数据。
还可通过HttpClient、Restlet、Apache CXF访问。



更多内容请关注微信公众号:全栈开发之技术栈

展开阅读全文

没有更多推荐了,返回首页