問題描述
在監聽器中,不管是 @Autowired
還是 @Resource
註解都無法獲取到服務
方法一
@PostConstruct
獲取 Bean
/**
* 會籤監聽器
* @author: linjinp
* @create: 2019-11-18 11:43
**/
@Component
public class MyListener implements ExecutionListener {
// 頁面配置參數注入
private FixedValue num;
@Autowired
private RuntimeService runtimeService;
private static MyListener myListener;
// 解決監聽器中 Bean 獲取不到問題
@PostConstruct
public void init() {
myListener = this;
myListener.runtimeService = this.runtimeService;
}
@Override
public void notify(DelegateExecution delegateExecution) {
// 調用 runtimeService 示例
ProcessInstance processInstance = myListener.runtimeService.createProcessInstanceQuery().processInstanceId(delegateExecution.getProcessInstanceId()).singleResult();
System.out.println(processInstance.getProcessDefinitionId());
// 獲取頁面配置參數的值
System.out.println(num.getExpressionText());
// 校驗 okNum 是否已經存在
if (!delegateExecution.hasVariable("okNum")) {
delegateExecution.setVariable("okNum", 0);
}
// 已審覈次數,審覈一次 +1
int okNum = (int) delegateExecution.getVariable("okNum") + 1;
delegateExecution.setVariable("okNum", okNum);
}
}
方法二
流程引擎服務通過 Context.getProcessEngineConfiguration()
獲取
import org.flowable.engine.impl.context.Context;
RuntimeService runtimeService = Context.getProcessEngineConfiguration().getRuntimeService();
自己的接口獲取
工具類
/**
* 獲取 Bean
* @author: linjinp
* @create: 2019-12-17 13:12
**/
@Component
public class SpringUtilConfig implements ApplicationContextAware {
/**
* 當前IOC
*
*/
private static ApplicationContext applicationContext;
/**
* * 設置當前上下文環境,此方法由spring自動裝配
*
*/
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
/**
* 從當前IOC獲取bean
*
* @return
*
*/
public static <T> T getBean(Class<T> c) {
return applicationContext.getBean(c);
}
}
調用
RequsetFunctionService requsetFunctionService = SpringUtilConfig.getBean(RequsetFunctionService.class);