Activiti服务任务委托表达错误

时间:2018-08-02 06:30:04

标签: activiti

protected ServiceTask createServiceTask(String id, String name, String ref) {
        ServiceTask serviceTask = new ServiceTask();
        serviceTask.setId(id);
        serviceTask.setName(name);
        serviceTask.setImplementation(ref);
        serviceTask.setImplementationType(ImplementationType.IMPLEMENTATION_TYPE_DELEGATEEXPRESSION);
        return serviceTask;
    }

public class ServiceTaskDelegate implements JavaDelegate {
    @Autowired
    private RepositoryService repositoryService;

    @Override
    public void execute(DelegateExecution delegateExecution) {
        System.out.println("service task execute");
        System.out.println(repositoryService);
    }
}

当我使用ImplementationType = ImplementationType.IMPLEMENTATION_TYPE_CLASS可以 ,但repositoryService == null

所以我使用ImplementationType = ImplementationType.IMPLEMENTATION_TYPE_DELEGATEEXPRESSION 扔掉

Caused by: org.activiti.engine.ActivitiIllegalArgumentException: Delegate expression com.test.activiti.ServiceTaskDelegate did neither resolve to an implementation of interface org.activiti.engine.impl.delegate.ActivityBehavior nor interface org.activiti.engine.delegate.JavaDelegate

springboot2.0 / activit6.0

2 个答案:

答案 0 :(得分:0)

  1. Spring支持 @Autowired 仅适用于Spring Bean。通常,Java类由Spring创建时会成为Spring Bean,但不是由 new 创建。
  2. 如果要使用 ImplementationType.IMPLEMENTATION_TYPE_DELEGATEEXPRESSION ,则应将已实现的接口更改为 ActivityBehavior

答案 1 :(得分:0)

于是我还是用了ImplementationType.IMPLEMENTATION_TYPE_CLASS 但是在获取bean的方式上不用@autowired或更自己写了getBean的方法代码如下:

@Component
public class SpringUtils implements ApplicationContextAware {

    private static ApplicationContext applicationContext = null;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        if(SpringUtils.applicationContext == null){
            SpringUtils.applicationContext  = applicationContext;
        }
    }

    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    public static Object getBean(String name){
        return getApplicationContext().getBean(name);

    }

    public static <T> T getBean(Class<T> clazz){
        return getApplicationContext().getBean(clazz);
    }

    public static <T> T getBean(String name, Class<T> clazz){
        return getApplicationContext().getBean(name, clazz);
    }

}

这样我就可以通过RuntimeService runtimeService = SpringUtils.getBean(RuntimeService.class);获取RuntimeService