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

在Flowable中如何在Java代码中获取activiti:expression调用Spring Bean

在Flowable中获取activiti:expression配置的Spring Bean

首先要明确:你当前BPMN文件里的saveToDb服务任务同时配置了activiti:class和activiti:expression,这两个属性是互斥的,Flowable只会生效其中一个,得先调整配置,再按以下方式获取Bean。

方案一:Spring依赖注入(推荐)

这种方式最简洁,直接把SaveToDbHandler交给Spring管理,通过依赖注入获取Bean:

  1. 给SaveToDbHandler添加Spring组件注解,让它成为Spring Bean:
import org.springframework.stereotype.Component;

@Component
public class SaveToDbHandler implements JavaDelegate {

    // 直接注入weatherInfoRepository
    private final WeatherInfoRepository weatherInfoRepository;

    // 构造方法注入(推荐写法)
    public SaveToDbHandler(WeatherInfoRepository weatherInfoRepository) {
        this.weatherInfoRepository = weatherInfoRepository;
    }

    @Override
    public void execute(DelegateExecution delegateExecution) {
        Map<?,?> weatherInfoOpt = (Map<?, ?>) delegateExecution.getVariable("weatherInfoMap");
        String name = (String) weatherInfoOpt.get("name");
        Double temperature = (Double) ((Map<?,?>) weatherInfoOpt.get("main")).get("temp");
        WeatherInfo weatherInfo = new WeatherInfo(name, temperature, new Date());
        
        // 使用注入的Repository保存数据
        weatherInfoRepository.save(weatherInfo);
        
        delegateExecution.setVariable("weatherInfo", weatherInfo);
    }
}
  1. 修改BPMN文件,去掉activiti:expression属性,只保留activiti:class:
<serviceTask id="saveToDb" activiti:exclusive="true" name="saveToDb" activiti:class="by.singularity.weatherforecast.service.SaveToDbHandler"/>

Flowable会自动从Spring容器中获取SaveToDbHandler实例,注入的weatherInfoRepository可直接使用。

方案二:从DelegateExecution中获取Spring Bean

如果不想把SaveToDbHandler转为Spring Bean,可以通过Flowable上下文获取Spring容器,再取出目标Bean:

修改SaveToDbHandler的execute方法:

import org.flowable.spring.SpringBeanFactory;

@Override
public void execute(DelegateExecution delegateExecution) {
    Map<?,?> weatherInfoOpt = (Map<?, ?>) delegateExecution.getVariable("weatherInfoMap");
    String name = (String) weatherInfoOpt.get("name");
    Double temperature = (Double) ((Map<?,?>) weatherInfoOpt.get("main")).get("temp");
    WeatherInfo weatherInfo = new WeatherInfo(name, temperature, new Date());
    
    // 获取Spring容器并取出Bean
    SpringBeanFactory springBeanFactory = (SpringBeanFactory) delegateExecution.getEngineConfiguration().getBeans();
    WeatherInfoRepository weatherInfoRepository = springBeanFactory.getBean(WeatherInfoRepository.class);
    
    // 使用Repository保存数据
    weatherInfoRepository.save(weatherInfo);
    
    delegateExecution.setVariable("weatherInfo", weatherInfo);
}

同时记得删除BPMN里的activiti:expression属性,避免冲突。

方案三:直接用activiti:expression调用Bean方法

如果保存逻辑简单,可省略JavaDelegate,直接在BPMN中通过表达式调用Bean方法:

修改BPMN的saveToDb服务任务:

<serviceTask id="saveToDb" activiti:exclusive="true" name="saveToDb" activiti:expression="${weatherInfoRepository.save(weatherInfo)}"/>

这种方式需要确保weatherInfo已作为流程变量存在,且weatherInfoRepository是Spring容器中的Bean,Flowable会自动解析表达式并调用对应方法。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.22 12:15:42