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了

然后就启动成功了。

Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'dynamicFlowController': Unsatisfied dependency expressed through field 'runtimeService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'runtimeServiceBean' defined in class path resource [org/activiti/spring/boot/DataSourceProcessEngineAutoConfiguration$DataSourceProcessEngineConfiguration.class]: Unsatisfied dependency expressed through method 'runtimeServiceBean' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'processEngine' defined in class path resource [org/activiti/spring/boot/DataSourceProcessEngineAutoConfiguration$DataSourceProcessEngineConfiguration.class]: Unsatisfied dependency expressed through method 'processEngine' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'springProcessEngineConfiguration' defined in class path resource [org/activiti/spring/boot/DataSourceProcessEngineAutoConfiguration$DataSourceProcessEngineConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.activiti.spring.SpringProcessEngineConfiguration]: Factory method 'springProcessEngineConfiguration' threw exception; nested exception is java.io.FileNotFoundException: class path resource [processes/] cannot be resolved to URL because it does not exist
06-09
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值