因项目需要需求,在网上找了很多关于activiti扩展用户自定义表的文章,发现在5.15.1中不起作用,查看源码之后发现在用会话工厂中的返回类型改成了接口而不是原来的抽象类。
1.CustomGroupManagerFactory 用户组会话工厂这边的返回类型要改成 GroupIdentityManager接口,之前的版本貌似是GroupEntityManager,因为在配置加载的时候是以这个为KEY值获得对应的工厂.
package tk.hecks.workflow; import org.activiti.engine.impl.interceptor.Session; import org.activiti.engine.impl.interceptor.SessionFactory; import org.activiti.engine.impl.persistence.entity.GroupEntityManager; import org.activiti.engine.impl.persistence.entity.GroupIdentityManager; import org.springframework.beans.factory.annotation.Autowired; /** * 自定义的Activiti用户组会话工厂 */ public class CustomGroupManagerFactory implements SessionFactory { private GroupEntityManager groupEntityManager; @Autowired public void setGroupEntityManager(GroupEntityManager groupEntityManager) { this.groupEntityManager = groupEntityManager; } public Class<?> getSessionType() { // 返回原始的GroupIdentityManager类型 return GroupIdentityManager.class; } public Session openSession() { // 返回自定义的GroupEntityManager实例 return groupEntityManager; } }
2. CustomUserManagerFactory 自定义的Activiti用户会话工厂
package tk.hecks.workflow; import org.activiti.engine.impl.interceptor.Session; import org.activiti.engine.impl.interceptor.SessionFactory; import org.activiti.engine.impl.persistence.entity.UserEntityManager; import org.activiti.engine.impl.persistence.entity.UserIdentityManager; import org.springframework.beans.factory.annotation.Autowired; /** * 自定义的Activiti用户会话工厂 */ public class CustomUserManagerFactory implements SessionFactory { private UserEntityManager userEntityManager; @Autowired public void setUserEntityManager(UserEntityManager userEntityManager) { this.userEntityManager = userEntityManager; } public Class<?> getSessionType() { // 返回原始的UserManager类型 return UserIdentityManager.class; } public Session openSession() { // 返回自定义的UserManager实例 return userEntityManager; } }
3.下面是用户组合用户管理器只要在里面实现自己的增删改查就行了
package tk.hecks.workflow; import java.util.List; import java.util.Map; import org.activiti.engine.identity.Group; import org.activiti.engine.identity.GroupQuery; import org.activiti.engine.impl.GroupQueryImpl; import org.activiti.engine.impl.Page; import org.activiti.engine.impl.persistence.entity.GroupEntity; import org.activiti.engine.impl.persistence.entity.GroupEntityManager; /** * 自定义的Activiti用户组管理器 * */ public class CustomGroupManager extends GroupEntityManager { @Override public Group createNewGroup(String groupId) { // TODO Auto-generated method stub return super.createNewGroup(groupId); } @Override public void insertGroup(Group group) { // TODO Auto-generated method stub super.insertGroup(group); } @Override public void updateGroup(GroupEntity updatedGroup) { // TODO Auto-generated method stub super.updateGroup(updatedGroup); } @Override public void deleteGroup(String groupId) { // TODO Auto-generated method stub super.deleteGroup(groupId); } @Override public GroupQuery createNewGroupQuery() { // TODO Auto-generated method stub return super.createNewGroupQuery(); } @Override public List<Group> findGroupByQueryCriteria(GroupQueryImpl query, Page page) { // TODO Auto-generated method stub return super.findGroupByQueryCriteria(query, page); } @Override public long findGroupCountByQueryCriteria(GroupQueryImpl query) { // TODO Auto-generated method stub return super.findGroupCountByQueryCriteria(query); } @Override public List<Group> findGroupsByUser(String userId) { // TODO Auto-generated method stub return super.findGroupsByUser(userId); } @Override public List<Group> findGroupsByNativeQuery(Map<String, Object> parameterMap, int firstResult, int maxResults) { // TODO Auto-generated method stub return super.findGroupsByNativeQuery(parameterMap, firstResult, maxResults); } @Override public long findGroupCountByNativeQuery(Map<String, Object> parameterMap) { // TODO Auto-generated method stub return super.findGroupCountByNativeQuery(parameterMap); } }
package tk.hecks.workflow; import java.util.List; import java.util.Map; import org.activiti.engine.identity.Group; 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; public class CustomUserManager extends UserEntityManager { @Override public User createNewUser(String userId) { // TODO Auto-generated method stub return super.createNewUser(userId); } @Override public void insertUser(User user) { // TODO Auto-generated method stub super.insertUser(user); } @Override public void updateUser(UserEntity updatedUser) { // TODO Auto-generated method stub super.updateUser(updatedUser); } @Override public UserEntity findUserById(String userId) { // TODO Auto-generated method stub return super.findUserById(userId); } @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) { 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); } }
4.最后是配置文件
<!-- 创建一个流程引擎的配置对象 --> <bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration"> <!--这里是数据源的指定 指向你自己的dataSource --> <property name="dataSource" ref="dataSource" /> <property name="transactionManager" ref="transactionManager" /> <!-- 设置数据库schema的更新方式 --> <property name="databaseSchemaUpdate" value="true" /> <!-- 是否启动jobExecutor --> <property name="jobExecutorActivate" value="false" /> <property name="activityFontName" value="宋体" /> <property name="labelFontName" value="宋体" /> <!-- 自动部署流程 --> <property name="deploymentResources"> <list> <value>classpath*:/diagrams/*</value> </list> </property> <property name="mailServerHost" value="smtp.xxx.com" /> <property name="mailServerPort" value="25" /> <property name="mailServerDefaultFrom" value="[email protected]" /> <property name="mailServerUsername" value="[email protected]" /> <property name="mailServerPassword" value="xxxx" /> <!-- 配置自定义用户 --> <property name="customSessionFactories"> <list> <bean class="tk.hecks.workflow.CustomGroupManagerFactory" > <property name="groupEntityManager"> <bean class="tk.hecks.workflow.CustomGroupManager"></bean> </property> </bean> <bean class="tk.hecks.workflow.CustomUserManagerFactory" > <property name="userEntityManager"> <bean id="userEntityManager" class="tk.hecks.workflow.CustomUserManager"></bean> </property> </bean> </list> </property> </bean>