【问题标题】:Spring Beans with Activiti BPMNSpring Beans 与 Activiti BPMN
【发布时间】:2016-02-01 12:59:51
【问题描述】:

我目前正在研究 Activiti 的 Spring bean 实现。我需要将一个 bean 注入到多个服务类中。

这是我尝试过的。

beans.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-3.0.xsd">

   <bean id="helloWorld" class="com.test.spring.HelloWorld" scope="singleton" autowire="byName">
       <property name="message" value="Hello World!"/>
   </bean>

</beans>

Bean 类

package com.test.spring;

public class HelloWorld {

     private String message;

       public void setMessage(String message){
          this.message  = message;
       }

       public void getMessage(){
          System.out.println("Your Message : " + message);
       }

}

Activiti 服务任务

public class ServiceTask1 implements JavaDelegate {

    @Override
    public void execute(DelegateExecution execution) throws Exception {
        System.out.println("Begin Trans : Execute:");

        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
          obj.getMessage();
          obj.setMessage("This is second message");
          obj.getMessage();
    }

}


public class ServiceTask2 implements JavaDelegate {

    @Override
    public void execute(DelegateExecution execution) throws Exception {
        System.out.println("Begin Trans : Execute:");

        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
          obj.getMessage();
    }

}

虽然 ServiceTask1 为消息设置了新值,但它并未反映在 ServiceTask2 中。原因可能是我也在 ServiceTask2 中创建了新的 ApplicationContext。有人可以[让我知道如何在多个 Activiti 服务任务中使用相同的单例 Bean。

【问题讨论】:

  • 实际上我不清楚那里提供的解决方案。有人可以解释一下我如何在我的情况下使用它。在每个服务任务中,我需要调用 Bean 的 getter 和 setter 方法,并根据 bean 类中的值进行一些其他处理。在那种情况下,activiti:expression 会解决我的问题吗?据我了解,我们只能在服务任务中使用 activiti:class 或 activiti:expression。

标签: spring activiti


【解决方案1】:

据我所知,您无法将 (@Autowire) bean 自动装配到 Java 委托中,因此为了访问您在应用程序配置中定义的 bean,您需要访问 applicationContext 并调用 getBean() 方法。

我通常会创建一个 ApplicationContext 提供程序类来简化此操作,因为您倾向于反复重用它。

这篇 Stackoverflow 帖子展示了如何创建这样的野兽:

Spring get current ApplicationContext

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多