You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Activiti5迁移Flowable6时customSessionFactories配置不生效问题求助

问题原因

你遇到的自定义SessionFactory不生效问题,主要是以下几个错误导致:

  1. SessionFactory接口实现不符合Flowable 6规范:Flowable 6的SessionFactory接口仅会调用带CommandContext参数的openSession方法,你自行添加的无参openSession方法不会被框架识别,同时你当前带参的openSession方法直接返回null,框架会自动 fallback 到默认的UserEntityManager实现,完全不会触发你的自定义逻辑。
  2. Bean重复定义导致依赖注入失效:你在FlowableUserManagerFactory类上加了@Service注解,同时又在Spring XML的customSessionFactories列表中手动定义了该类的实例,会导致XML中实例的@Autowired依赖全部为null,就算被调用也会抛出空指针异常。
  3. 自定义UserEntityManager的核心方法返回null:你实现的FlowableUserManager中,框架调用的findById、无参findUserByQueryCriteria方法直接返回null,就算前面的配置正确也无法拿到查询结果。
  4. 兼容模式可能拦截请求:你开启了flowable5CompatibilityEnabled兼容模式,如果请求被转发到Activiti5兼容层处理,你注册到Flowable6引擎的自定义SessionFactory不会生效。
  5. 代码存在编译错误:FlowableUserManager的带Page参数findUserByQueryCriteria方法中重复定义了userQuery变量,会导致编译失败,逻辑无法执行。

解决步骤

1. 修正FlowableUserManagerFactory实现

删除类上的@Service注解,把逻辑迁移到框架要求的openSession重载方法中:

public class FlowableUserManagerFactory implements SessionFactory {

    @Autowired
    private IAuthorizationService<Long, Long, Long> authorizationService;
    @Autowired
    private IAuthenticationService<Long, Long> authenticationService;
    @Autowired
    private IGroupToActivityRoleConverter groupToActivityConverter;

    @Override
    public Class<?> getSessionType() {
        // 注意此处必须返回Flowable 6官方的org.flowable.idm.engine.impl.persistence.entity.UserEntityManager,不可导错Activiti5的同名类
        return UserEntityManager.class;
    }

    @Override
    public Session openSession(CommandContext commandContext) {
        return new FlowableUserManager(authorizationService, authenticationService, groupToActivityConverter);
    }
}

2. 修正FlowableUserManager的空实现

补全框架会调用的核心方法,同时修复重复变量的编译错误:

// 补全findById方法实现
@Override
public UserEntity findById(String userId) {
    return findUserById(userId);
}

// 补全无Page参数的findUserByQueryCriteria实现
@Override
public List<org.flowable.idm.api.User> findUserByQueryCriteria(UserQueryImpl userQuery) {
    return findUserByQueryCriteria(userQuery, null);
}

// 修复重复定义userQuery的编译错误
public List<org.flowable.idm.api.User> findUserByQueryCriteria(UserQueryImpl query, Page page) {
    List<org.flowable.idm.api.User> userList = new ArrayList<>();
    // 删掉重复的UserQueryImpl userQuery = (UserQueryImpl) query; 直接使用query参数
    if (!StringUtils.isEmpty(query.getId())) {
        userList.add(findUserById(query.getId()));
        return userList;
    } else if (!StringUtils.isEmpty(query.getEmail())) {
        userList.add(findUserByEmail(query.getEmail()));
        return userList;
    } 
    // 其余原有逻辑保持不变
    // ...
}

3. 调整Spring配置

如果不需要兼容Activiti5的旧流程实例,直接删除以下三个配置,避免兼容层拦截身份查询请求:

<!-- 删除这三个配置 -->
<property name="flowable5CompatibilityEnabled" value="true" />
<property name="flowable5CompatibilityHandlerFactory" ref="flowable5CompatibilityFactory" />
<bean id="flowable5CompatibilityFactory" class="org.activiti.compatibility.spring.SpringFlowable5CompatibilityHandlerFactory" />

如果必须保留兼容模式,需要将自定义SessionFactory同时注册到兼容层的ProcessEngineConfiguration中。

4. 验证配置生效

可以在FlowableUserManagerFactory的getSessionType方法中添加断点,引擎启动时如果触发该断点,说明自定义SessionFactory已经被正确注册。


内容的提问来源于stack exchange,提问作者Andreu_C

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.01 11:06:00