title: Spring Beans with Activiti BPMN
body: <p>I am currently looking in to Spring bean implementation with Activiti. I need to inject a bean to multiple service classes.</p>
  
  <p>Here is what I have tried.</p>
  
  <p><strong>beans.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  
  &lt;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"&gt;
  
     &lt;bean id="helloWorld" class="com.test.spring.HelloWorld" scope="singleton" autowire="byName"&gt;
         &lt;property name="message" value="Hello World!"/&gt;
     &lt;/bean&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p><strong>Bean Class</strong> </p>
  
  <pre><code>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);
         }
  
  }
  </code></pre>
  
  <p><strong>Activiti Service Tasks</strong></p>
  
  <pre><code>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();
      }
  
  }
  </code></pre>
  
  <p>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.</p>