【问题标题】:failed to Inject Activiti framework services in my Spring webapp未能在我的 Spring webapp 中注入 Activiti 框架服务
【发布时间】:2015-06-12 17:08:02
【问题描述】:

我正在尝试使用 Activiti 和 Spring MVC 框架在我的 Web 应用程序中创建工作流,因此我必须通过 applicationContext.xml 文件注入 Activiti 服务,然后自动装配这些服务。

这是 JUnit 测试后控制台中的问题

GRAVE:在允许 TestExecutionListener 时捕获异常 [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@14c8743] 准备测试实例 [tds_erp.testSpring@144719c] org.springframework.beans.factory.BeanCreationException:错误 创建名为“tds_erp.testSpring”的bean:注入自动装配 依赖失败;嵌套异常是 org.springframework.beans.factory.BeanCreationException:不能 自动装配字段:org.activiti.engine.RepositoryService tds_erp.testSpring.repositoryService;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException: 否 找到 [org.activiti.engine.RepositoryService] 类型的合格 bean 对于依赖项:预计至少有 1 个符合自动装配条件的 bean 这种依赖的候选人。依赖注解: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

引起:org.springframework.beans.factory.BeanCreationException: 无法自动装配字段:org.activiti.engine.RepositoryService tds_erp.testSpring.repositoryService;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException: 否 找到 [org.activiti.engine.RepositoryService] 类型的合格 bean 对于依赖项:预计至少有 1 个符合自动装配条件的 bean 这种依赖的候选人。依赖注解: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

原因: org.springframework.beans.factory.NoSuchBeanDefinitionException: 否 找到 [org.activiti.engine.RepositoryService] 类型的合格 bean 对于依赖项:预计至少有 1 个符合自动装配条件的 bean 这种依赖的候选人。依赖注解: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

应用上下文.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
        ">


    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy">
        <property name="targetDataSource">
            <bean class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
                <property name="driverClassName" value="com.mysql.jdbc.Driver" />
                <property name="url" value="jdbc:mysql://localhost:3306/activiti" />
                <property name="username" value="root" />
                <property name="password" value="admin" />
            </bean>
        </property>
    </bean>
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
    <bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">
        <property name="databaseType" value="h2" />
        <property name="dataSource" ref="dataSource" />
        <property name="transactionManager" ref="transactionManager" />
        <property name="databaseSchemaUpdate" value="true" />
        <property name="deploymentResources"
            value="classpath*:chapter4/bookorder.spring.bpmn20.xml" />
        <property name="jobExecutorActivate" value="false" />
    </bean>
    <bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean">
        <property name="processEngineConfiguration" ref="processEngineConfiguration" />
    </bean>
    <bean id="repositoryService" factory-bean="processEngine"
        factory-method="getRepositoryService" />
    <bean id="runtimeService" factory-bean="processEngine"
        factory-method="getRuntimeService" />
    <bean id="taskService" factory-bean="processEngine"
        factory-method="getTaskService" />
    <bean id="historyService" factory-bean="processEngine"
        factory-method="getHistoryService" />
    <bean id="managementService" factory-bean="processEngine"
        factory-method="getManagementService" />
</beans>

JUnit 测试文件:

import static org.junit.Assert.assertEquals;

import java.util.HashMap;
import java.util.Map;

import org.activiti.bpmn.model.Task;
import org.activiti.engine.FormService;
import org.activiti.engine.HistoryService;
import org.activiti.engine.IdentityService;
import org.activiti.engine.ManagementService;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngineConfiguration;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.spring.ProcessEngineFactoryBean;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath*:applicatioContext.xml")

public class testSpring {

    @Autowired(required=true)
     RepositoryService repositoryService;
    @Autowired(required=true)
     RuntimeService runtimeService;
    @Autowired(required=true)
     TaskService taskService;


    @Test
    public void simpleSpringTest() {

    Map<String, Object> variableMap =
    new HashMap<String, Object>();
    variableMap.put("object", "Activiti in Action");
    variableMap.put("description", "123456");
    runtimeService.startProcessInstanceByKey(
    "employeeRequest", variableMap);

    Task task = (Task) taskService
    .createTaskQuery().taskAssignee("kermit")
    .singleResult();
    assertEquals("Complete order", task.getName());
    taskService.complete(task.getId());
    assertEquals(0, runtimeService.
    createProcessInstanceQuery().count());
    }
    }

请帮帮我!

【问题讨论】:

    标签: java spring spring-mvc dependency-injection activiti


    【解决方案1】:

    您似乎想在测试用例中使用applicatioContext.xml 中定义的bean,例如repositoryService 等。但是你在the @ContextConfiguration 中声明的只是活动的默认配置文件activiti.cfg.xml 您可以在测试中尝试以下内容吗:

    @ContextConfiguration("classpath:applicatioContext.xml")
    

    只是一个建议:为类似于 webapplicationContext 中的测试上下文创建一个单独的applicatioContext.xml 可能是个好主意。这将有助于保留不同的数据等,您可以比正常的开发数据库实例更好地管理数据的前/后条件。您甚至可能想为测试本身生成一个数据库实例并在测试后拆除。

    编辑: 您可以像这样提供多个 conf 文件 - @ContextConfiguration(locations = { "classpath:applicatioContext.xml" , "classpath*:activiti.cfg.xml"})。检查这个post

    通常定义“&lt;bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration"&gt;”应在内部加载activiti.cfg.xml,除非您更改了其默认位置。

    【讨论】:

    • 感谢回答,但我有 2 个配置文件,applicatioContext.xmlactiviti.cfg.xml,我试过了,但同样的问题
    • 我尝试了您的建议,但我不知道在与 applicationContext 分离后如何调用 activiti 配置。它说没有定义 bean
    • 感谢您的回答,通过将这一行 &lt;bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration"&gt; 放在 Activiti 配置的第一个来解决,所以我知道 ProcessEngine 应该作为第一个加载,顺序很重要跨度>
    • 我很高兴它成功了。据我所知,bean定义的顺序应该无关紧要。我认为这是导致问题的其他原因。但我不能说你修复的信息有限:)。但很乐意提供帮助。
    • 我已经编辑了来自 cmets 的答案。你能接受答案吗?
    猜你喜欢
    • 1970-01-01
    • 2016-05-17
    • 2014-03-13
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    • 2017-08-08
    • 2014-02-21
    • 2015-05-05
    相关资源
    最近更新 更多