如何在Activiti JavaDelegate中访问spring bean? [英] How can I access a spring bean in Activiti JavaDelegate?

查看:943
本文介绍了如何在Activiti JavaDelegate中访问spring bean?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to get a simple Spring example to work with Activiti 5.5, and having some trouble. I'm using the process engine configured with activiti under %activiti_home%/apps/apache-tomcat-6.0.32/webapps/activiti-rest.

我修改了弹簧配置文件,以使其包含我的自定义spring配置文件:

I modified the spring config file so that it performs an include of my custom spring configuration file:

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

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

I deployed my applicationContext.xml file to the activiti-rest/WEB-INF/classes folder. Activiti starts up fine, and I see the System.out.println in my bean constructor, so I know that my spring config is being read and the bean is being constructed. I created a spring bean for the class that implements JavaDelegate and injected my bean to it and it always comes up null.

这是我的Spring Config:

Here is my 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>

这是我的豆子:

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:

Here is my class that implements the 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,而是在创建新实例。

This all seems pretty straightforward to me, however I think the problem is that Activiti is not using a spring bean in the class that is being invoked on my JavaService task, it is creating a new instance.

<?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实体?

How do I get a reference to a Spring Bean either a simple one such as I have here, or one that has been configured as a JPA Entity?

任何/所有答复都受到赞赏!

Any/All replies appreciated!

6.28.2011
更新:
在尝试将activiti-rest应用更改为使用SpringProcessEngineConfiguration而不是独立的StandaloneProcessEngineConfiguration时,我在其中更改了activiti-cfg.xml文件。 activiti-cfg.jar文件并重新启动了Tomcat。

6.28.2011 Updated: In trying to change the activiti-rest app to use the SpringProcessEngineConfiguration instead of the standalone StandaloneProcessEngineConfiguration, I changed the activiti-cfg.xml file in the activiti-cfg.jar file and restarted Tomcat.

我将xml文件更改为:

I change the xml file to look like this:

<?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时,没有出现异常,但是当我启动Explorer并尝试登录时,我得到了以下异常:

When I restarted Tomcat no exceptions appear, however when I bring up Explorer and try to login, I get the following Exception:

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)


推荐答案

我的一个项目在春季使用Activiti。我认为JavaDelagate可能是问题所在。您可以按以下方式从activiti的服务任务中调用每个Spring bean:

One of my project uses Activiti with spring. I think that JavaDelagate can be the problem. You can call from activiti's service task every spring bean this way:

bean定义:

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

activiti xml:

activiti xml:

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

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

You can also pass parameters to the functions for example process variables:

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

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

I always use service tasks this way, and haven't got problem with singleton beans. Hope it helps. Please take a comment, if I didn't understand your problem.

更新:

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

My project uses activiti like an embedded workflow engine. Activiti uses the same applicationContext with my webapp.

我的流程引擎配置:

<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>

这篇关于如何在Activiti JavaDelegate中访问spring bean?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆