1

I am currently looking in to Spring bean implementation with Activiti. I need to inject a bean to multiple service classes.

Here is what I have tried.

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 Class

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

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();
    }

}

Although ServiceTask1 set new value to the message it does not reflected in ServiceTask2. The reason may I'm creating new ApplicationContext in ServiceTask2 as well. Can someone [lease let me know how to use same singleton Bean accross multiple Activiti service Tasks.

Anupama Pathirage
  • 631
  • 2
  • 10
  • 22
  • 1
    Possible duplicate of [How can I access a spring bean in Activiti JavaDelegate?](http://stackoverflow.com/questions/6474324/how-can-i-access-a-spring-bean-in-activiti-javadelegate) – lmazgon Feb 01 '16 at 13:07
  • Actually I'm not clear with the solution provided there. Can someone kindly explain a bit how I can use this for my case. In each service task I need to call getter and setter methods of the Bean and do some other processing based on the values in the bean class. In that case will activiti:expression solve my problem? As I understood we can use either activiti:class or activiti:expression only with the service task. – Anupama Pathirage Feb 01 '16 at 16:43

1 Answers1

1

You cannot autowire (@Autowire) beans into a Java Delegate as fas as I know, so in order to access beans you define in you application configuration you will need to access the applicationContext and call the getBean() method.

I typically create an ApplicationContext provider class to make this easy, since you tend to reuse it over an over.

This Stackoverflow post shows how to create such a beast:

Spring get current ApplicationContext

Community
  • 1
  • 1
Greg Harley
  • 3,230
  • 1
  • 11
  • 14