如何在Activiti JavaDelegate中访问spring bean?

时间:2011-06-24 22:15:41

标签: spring workflow javabeans activiti

我正在尝试使用一个简单的Spring示例来使用Activiti 5.5,并遇到一些麻烦。我正在使用在%activiti_home%/ apps / apache-tomcat-6.0.32 / webapps / activiti-rest下配置了activiti的流程引擎。

我修改了spring配置文件,以便它执行我的自定义spring配置文件的包含:

<import resource="classpath*:applicationContext*.xml"/> 

我将applicationContext.xml文件部署到activiti-rest / WEB-INF / classes文件夹。 Activiti启动正常,我在我的bean构造函数中看到System.out.println,所以我知道正在读取我的spring配置并且正在构造bean。我为实现JavaDelegate的类创建了一个spring bean,并将bean注入其中,并且它总是为null。

这是我的Spring Config:

<?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:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx" 
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

    <bean id="myBean" class="org.bpmn.examples.MyBean"/>
    <bean id="taskBean" class="org.bpmn.examples.GetBeanTest">
            <property name="myBean" ref="myBean"/>
    </bean>
</beans>

这是我的Bean:

package org.bpmn.examples;

import java.io.Serializable;

public class MyBean implements Serializable {

    public MyBean() {
        super();
        System.out.println("<========================== myBean ===========================>");
        System.out.println("<========================== myBean ===========================>");
        System.out.println("<========================== myBean ===========================>");
    }
    /**
     * 
     */
    private static final long serialVersionUID = -2867207654072787909L;
    Long id;
    String description;

    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }

}

这是我实现JavaDelegate的类:

package org.bpmn.examples;

import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.JavaDelegate;

public class GetBeanTest implements JavaDelegate {

    private MyBean myBean;

    @Override
    public void execute(DelegateExecution execution) throws Exception {
        if(myBean == null){
            System.out.println("Bean was null!");
        }else{
            System.out.println("Bean is valid!");
        }

    }

    public void setMyBean(MyBean myBean) {
        this.myBean = myBean;
    }
}

这对我来说似乎很简单,但我认为问题是Activiti没有在我的JavaService任务上调用的类中使用spring bean,它正在创建一个新实例。

<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:activiti="http://activiti.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC" xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI" typeLanguage="http://www.w3.org/2001/XMLSchema" expressionLanguage="http://www.w3.org/1999/XPath" targetNamespace="http://www.activiti.org/test">
  <process id="TestSpringConfig" name="TestSpringConfig">
    <documentation>Place documentation for the 'TestSpringConfig' process here.</documentation>
    <startEvent id="startevent1" name="Start"></startEvent>
    <serviceTask id="servicetask1" name="BeanTest" activiti:class="org.bpmn.examples.GetBeanTest"></serviceTask>
    <endEvent id="endevent1" name="End"></endEvent>
    <sequenceFlow id="flow1" name="" sourceRef="startevent1" targetRef="servicetask1"></sequenceFlow>
    <sequenceFlow id="flow2" name="" sourceRef="servicetask1" targetRef="endevent1"></sequenceFlow>
  </process>
</definitions>

如何获得对Spring Bean的引用,例如我在这里的简单引用,或者已经配置为JPA实体的引用?

任何/所有回复都表示赞赏!


2011年6月28日 更新: 在尝试更改activiti-rest应用程序以使用SpringProcessEngineConfiguration而不是独立的StandaloneProcessEngineConfiguration时,我更改了activiti-cfg.jar文件中的activiti-cfg.xml文件并重新启动了Tomcat。

我将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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
        <property name="driverClass" value="org.h2.Driver" />
        <property name="url" value="jdbc:h2:tcp://localhost/activiti" />
        <property name="username" value="sa" />
        <property name="password" value="" />
    </bean>

  <bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration"/>
  <bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean">
    <property name="processEngineConfiguration" ref="processEngineConfiguration" />
  </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="dataSource" ref="dataSource" />
    <property name="transactionManager" ref="transactionManager" />
    <property name="databaseSchemaUpdate" value="true" />
    <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>

当我重新启动Tomcat时,没有出现异常,但是当我启动资源管理器并尝试登录时,我得到以下异常:

INFO: Server startup in 12011 ms
10:32:02,338  ERROR [extensions.webscripts.AbstractRuntime] Exception from executeScript - redirecting to status template error: 05280000 Wrapped Exception (with status template): null
org.springframework.extensions.webscripts.WebScriptException: 05280000 Wrapped Exception (with status template): null
    at org.springframework.extensions.webscripts.AbstractWebScript.createStatusException(AbstractWebScript.java:742)
    at org.springframework.extensions.webscripts.DeclarativeWebScript.execute(DeclarativeWebScript.java:167)

3 个答案:

答案 0 :(得分:12)

我的一个项目使用Activiti和spring。我认为JavaDelagate可能是个问题。您可以通过这种方式从每个spring bean调用activiti的服务任务:

bean定义:

<bean id="exampleBean" class="org.bpmn.examples.ExampleBean"/>

activiti xml:

<serviceTask id="servicetask" name="Example" activiti:expression="${exampleBean.doSomething()}"></serviceTask>

您还可以将参数传递给函数,例如过程变量:

<serviceTask id="servicetask" name="Example" activiti:expression="${exampleBean.doSomething(processVariable)}"></serviceTask>

我总是以这种方式使用服务任务,并且没有单例bean的问题。 希望能帮助到你。如果我不明白你的问题,请发表评论。

<强>更新

我的项目像嵌入式工作流引擎一样使用activiti。 Activiti使用与我的webapp相同的applicationContext。

我的流程引擎配置:

<bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">
        <property name="databaseType" value="mssql" />
        <property name="dataSource" ref="dataSource" />
        <property name="transactionManager" ref="transactionManager" />
        <property name="databaseSchemaUpdate" value="true" />
        <property name="jobExecutorActivate" value="true" />
        <property name="deploymentResources" value="classpath*:/diagrams/*.bpmn20.xml" />           
    </bean> 


    <bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean">
        <property name="processEngineConfiguration" ref="processEngineConfiguration" />
    </bean>

答案 1 :(得分:1)

我正在使用 @Autowired

引入我的依赖项。由于JavaDelegate没有被Spring实例化,我调用

applicationContext.getAutowireCapableBeanFactory().autowireBean(this);

在我的Delegate的超类的构造函数中,它将所有依赖项注入到Delegate中。您可能想知道从哪里获取applicationContext,http://sujitpal.blogspot.com/2007/03/accessing-spring-beans-from-legacy-code.html为您提供答案。

答案 2 :(得分:0)

我的猜测是,activiti确实会创建一个新实例,因为activiti不知道它应该从spring容器中检索一个实例。

如果您尚未检查此资源:

http://www.activiti.org/userguide/index.html#springintegration

也许这就是你需要的东西(即ProcessEngineFactoryBean)