Activiti步步踩坑之二:Unsatisfied dependency expressed through field ‘repositoryService‘

下面是Activiti创建流程代码,我是跟着网上教程一步步走的但是总是出问题,每次都是找很久办法才解决问题,现在把解决方式分享给大家:

@SpringBootTest
@RunWith(SpringRunner.class)
public class VocationDemo {
    @Autowired
    private RepositoryService repositoryService;
    @Autowired
    private RuntimeService runtimeService;
    @Autowired
    private TaskService taskService;
    @Test
    public void demoTest() {
        // 1、发布流程
        Deployment deployment = repositoryService.createDeployment().name("请假流程").addClasspathResource("processes/vocationDemo.bpmn20.xml").deploy();
        // 2、启动一个流程实例,由于两个环节审批的人都是写死的test,所以这边在启动流程的时候未透传下一环节处理人
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("vocation");
        // 3、查询所有任务
        List<Task> tasks = taskService.createTaskQuery().processInstanceId(processInstance.getProcessInstanceId()).list();
        // 4、提交到总经理审批即完成任务,同样不需要传递变量
        Task task = tasks.get(0);
        taskService.complete(task.getId());
    }
}

通过springboot启动activiti时报如下错误:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 
'com.example.activitidemo2.VocationDemo': Unsatisfied dependency expressed through 
field 'repositoryService': No qualifying bean of type 'org.activiti.engine.RepositoryService' available:
 expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:767) ~[spring-beans-6.0.13.jar:6.0.13]

这个错误表明Spring框架在尝试创建VocationDemo类的一个bean实例时,无法找到可以注入RepositoryService类型的bean这通常是因为在你的Spring配置中没有定义RepositoryService类型的bean,或者在你的类没有被Spring框架管理

所以我们需要给RepositoryService注入Bean
创建一个Activiti配置类

@Configuration
public class ActivitiConfig {

        @Bean
        public RepositoryService repositoryService() {
            return ProcessEngines.getDefaultProcessEngine().getRepositoryService();
        }

        @Bean
        public RuntimeService runtimeService() {
            return ProcessEngines.getDefaultProcessEngine().getRuntimeService();
        }

        @Bean
        public TaskService taskService() {
            return ProcessEngines.getDefaultProcessEngine().getTaskService();
        }

}

这就给这三个函数注入了bean

在测试类上加上这个注解@SpringBootTest(classes = ActivitiConfig.class) 告诉Spring Boot在运行测试时需要考虑ActivitiConfig类中的配置这样,Spring就能找到并注入需要的bean了

然后就启动成功了。

你可能感兴趣的:(Activiti,java,spring,boot)