Activiti测试报错:org.springframework.beans.factory.UnsatisfiedDependencyException

Spring boot 3.x Activiti7 启动失败,并且在一开始导入依赖时无法自动创建表(看到最后)

使用springboot3.x + Activiti7后,发现工作流模块无法启动,并且在一开始导入依赖时无法自动创建表。

1. 具体报错如下:

java 复制代码
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.hgh.auth.activiti.ProcessTest': 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]
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:747) ~[spring-beans-6.0.13.jar:6.0.13]
	at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:145) ~[spring-beans-6.0.13.jar:6.0.13]
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:492) ~[spring-beans-6.0.13.jar:6.0.13]
......

2. 运行代码如下:

java 复制代码
@SpringBootTest
public class ProcessTest {

    //注入RepositoryService
    @Autowired
    private RepositoryService repositoryService;
    
    //单个文件部署
    @Test
    public void deployProcess() {
        //流程部署
        Deployment deploy = repositoryService.createDeployment()
                .addClasspathResource("process/qingjia.bpmn20.xml")
                .addClasspathResource("process/qingjia.png")
                .name("请假申请流程001")
                .deploy();
        System.out.println(deploy.getId());
        System.out.println(deploy.getName());
    }
}

3. 报错原因:

这个错误表明Spring框架在尝试创建ProcessTest类的一个bean实例时,无法找到可以注入RepositoryService类型的bean这通常是因为在你的Spring配置中没有定义RepositoryService类型的bean,或者在类没有被Spring框架管理。即流程引擎相关类启动时无法正常注入。

4. 解决办法:

方法1

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

java 复制代码
/**
 * desc Activiti配置类,为RepositoryService注入Bean
 * @author GreyPigeon mail:2371849349@qq.com
 * @since 2024-04-03-9:25
 **/
@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了

结果:失败

方法二:

对于mysql-connector-java版本 ≥ 8.x,需在数据库访问的地址上添加:nullCatalogMeansCurrent=true

如jdbc:mysql://localhost:3306/activiti?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&nullCatalogMeansCurrent=true

参考:https://blog.csdn.net/qq_41883461/article/details/134518576

结果:失败

最后发现:

是因为 springboot3 自动配置注册机制中,自动配置注册的spring.factories写法已废弃,已改为META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports

方法三:

在resources下新建META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports

imports文件内填入以下内容,然后重启服务。

java 复制代码
org.activiti.common.util.conf.ActivitiCoreCommonUtilAutoConfiguration
org.activiti.application.conf.ApplicationAutoConfiguration
org.activiti.core.common.spring.connector.autoconfigure.ConnectorAutoConfiguration
org.activiti.core.common.spring.identity.config.ActivitiSpringIdentityAutoConfiguration
org.activiti.core.common.spring.project.conf.ProjectModelAutoConfiguration
org.activiti.spring.resources.conf.ResourceFinderAutoConfiguration
org.activiti.core.common.spring.security.config.ActivitiSpringSecurityAutoConfiguration
org.activiti.core.common.spring.security.policies.config.ActivitiSpringSecurityPoliciesAutoConfiguration
org.activiti.api.runtime.conf.impl.CommonModelAutoConfiguration
org.activiti.api.runtime.conf.impl.ProcessModelAutoConfiguration
org.activiti.runtime.api.conf.ConnectorsAutoConfiguration
org.activiti.runtime.api.conf.ProcessRuntimeAutoConfiguration
org.activiti.core.common.spring.connector.autoconfigure.ConnectorAutoConfiguration
org.activiti.spring.process.conf.ProcessExtensionsConfiguratorAutoConfiguration
org.activiti.runtime.api.conf.CommonRuntimeAutoConfiguration
org.activiti.api.task.conf.impl.TaskModelAutoConfiguration
org.activiti.runtime.api.conf.TaskRuntimeAutoConfiguration
org.activiti.application.conf.ApplicationProcessAutoConfiguration
org.activiti.spring.boot.EndpointAutoConfiguration
org.activiti.spring.boot.ProcessEngineAutoConfiguration
org.activiti.spring.boot.ActivitiMethodSecurityAutoConfiguration
org.activiti.spring.process.conf.ProcessExtensionsAutoConfiguration

结果:运行成功,且数据库也创建成功

vitiMethodSecurityAutoConfiguration

org.activiti.spring.process.conf.ProcessExtensionsAutoConfiguration

结果:运行成功,且数据库也创建成功
> 参考:https://blog.csdn.net/sinat_35670743/article/details/130964792?
相关推荐
哎呦没1 分钟前
SpringBoot框架下的资产管理自动化
java·spring boot·后端
2401_857600954 分钟前
SpringBoot框架的企业资产管理自动化
spring boot·后端·自动化
m0_571957582 小时前
Java | Leetcode Java题解之第543题二叉树的直径
java·leetcode·题解
一点媛艺3 小时前
Kotlin函数由易到难
开发语言·python·kotlin
姑苏风3 小时前
《Kotlin实战》-附录
android·开发语言·kotlin
奋斗的小花生4 小时前
c++ 多态性
开发语言·c++
魔道不误砍柴功4 小时前
Java 中如何巧妙应用 Function 让方法复用性更强
java·开发语言·python
NiNg_1_2344 小时前
SpringBoot整合SpringSecurity实现密码加密解密、登录认证退出功能
java·spring boot·后端
闲晨4 小时前
C++ 继承:代码传承的魔法棒,开启奇幻编程之旅
java·c语言·开发语言·c++·经验分享
种树人202408194 小时前
如何在 Spring Boot 中启用定时任务
spring boot