很难将Activiti配置成Springboot应用api

Difficulty to configure Activiti into a Springboot application api

首先,将 Activiti 嵌入到 API 类型的应用程序中以便在该应用程序中使用是否可行,或者 Activiti 是否应该 运行 独立?

下面的错误是由于 bean 定义引起的,但我不确定应该在哪里定义 bean 以及如何定义 - 如果这是版本 6 的正确方法。我们的 Springhboot 2 标准是在 java 而不是 xml 上下文

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2019-04-10 21:17:43.924 ERROR 19516 --- [  restartedMain] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Field runtimeService in ma.cvmeeting.workflow.WorkflowApplication$MyrestController required a bean of type 'org.activiti.engine.RuntimeService' that could not be found.

The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean of type 'org.activiti.engine.RuntimeService' in your configuration.


Process finished with exit code 0

代码:

import org.activiti.engine.RuntimeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
public class WorkflowApplication {

public static void main(String[] args) {
    SpringApplication.run(WorkflowApplication.class, args);
}



@RestController
public static class MyrestController{


    @Autowired
    private RuntimeService runtimeService;


    @GetMapping("/start-process")
    public String startProcess() {

        runtimeService.startProcessInstanceByKey("Potulerauneoffre");
        return "Process started. Number of currently running"
                + "process instances = "
                + runtimeService.createProcessInstanceQuery().count();
    }
}

pom.xml:

<project>

    <groupId>ma.cvmeeting</groupId>
    <artifactId>workflow</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>workflow</name>
    <description>Demo project for Spring Boot</description>

   <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.activiti</groupId>
            <artifactId>activiti-engine</artifactId>
            <version>7-201802-EA</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.thymeleaf</groupId>
            <artifactId>thymeleaf</artifactId>
            <version>3.0.11.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.h2database</groupId>
            <artifactId>h2database</artifactId>
            <version>1.0.20061217</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

当您将引擎嵌入到基于 spring 的应用程序中时,有两种初始化引擎的方法: 1.) 让 spring 为您初始化它,以便您无需任何配置即可立即使用所有引擎服务。这需要 activiti-spring-boot-starter 作为依赖项。 2.) 您自己初始化引擎并提供来自@Configuration class 的服务bean。为此,您只需要 activiti-engine 核心作为依赖项

您的应用程序找不到 RuntimeService 的原因是您正在尝试第二种方法,在您的 pom.xml 中添加以下依赖项并删除引擎 one

<dependency>
    <groupId>org.activiti</groupId>
    <artifactId>activiti-spring-boot-starter</artifactId>
</dependency>

您应该关注 documentation 以获得更多帮助。

您可以编写一个@Configuration class 并定义 Activiti 服务,如下所示:

@Configuration
public class ActivityConfig {

    @Autowired
    DataSource dataSource;

    @Bean
    public DataSourceTransactionManager getTransactionManager() {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean
    public ProcessEngineConfigurationImpl getProcessEngineConfiguration() {
        SpringProcessEngineConfiguration res = new SpringProcessEngineConfiguration();
        res.setDataSource(dataSource);
        res.setTransactionManager(getTransactionManager());
        return res;
    }

    @Bean
    public ProcessEngineFactoryBean getProcessEngine() {
        ProcessEngineFactoryBean res = new ProcessEngineFactoryBean();
        res.setProcessEngineConfiguration(getProcessEngineConfiguration());
        return res;
    }

    @Bean
    public RepositoryService getRepositoryService() throws Exception {
        return getProcessEngine().getObject().getRepositoryService();
    }

    @Bean
    public FormService getFormService() throws Exception {
        return getProcessEngine().getObject().getFormService();
    }

    @Bean
    public TaskService getTaskService() throws Exception {
        return getProcessEngine().getObject().getTaskService();
    }

    @Bean
    public RuntimeService getRuntimeService() throws Exception {
        return getProcessEngine().getObject().getRuntimeService();
    }

    @Bean
    public HistoryService getHistoryService() throws Exception {
        return getProcessEngine().getObject().getHistoryService();
    }

    @Bean
    public IdentityService getIdentityService() throws Exception {
        return getProcessEngine().getObject().getIdentityService();
    }
}

如果您打算使用 spring 启动 2.x 和使用新的 API,我们推荐使用 activiti 7 核心。如果您想参与新的 API 和项目计划,这是个好时机