title: How can I access a spring bean in Activiti JavaDelegate?
body: <p>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.</p>
  
  <p>I modified the spring config file so that it performs an include of my custom spring configuration file:</p>
  
  <pre><code>&lt;import resource="classpath*:applicationContext*.xml"/&gt; 
  </code></pre>
  
  <p>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.</p>
  
  <p>Here is my Spring Config:</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"
         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"&gt;
  
      &lt;bean id="myBean" class="org.bpmn.examples.MyBean"/&gt;
      &lt;bean id="taskBean" class="org.bpmn.examples.GetBeanTest"&gt;
              &lt;property name="myBean" ref="myBean"/&gt;
      &lt;/bean&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p>Here is my Bean:</p>
  
  <pre><code>package org.bpmn.examples;
  
  import java.io.Serializable;
  
  public class MyBean implements Serializable {
  
      public MyBean() {
          super();
          System.out.println("&lt;========================== myBean ===========================&gt;");
          System.out.println("&lt;========================== myBean ===========================&gt;");
          System.out.println("&lt;========================== myBean ===========================&gt;");
      }
      /**
       * 
       */
      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;
      }
  
  }
  </code></pre>
  
  <p>Here is my class that implements the JavaDelegate:</p>
  
  <pre><code>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;
      }
  }
  </code></pre>
  
  <p>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.  </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;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"&gt;
    &lt;process id="TestSpringConfig" name="TestSpringConfig"&gt;
      &lt;documentation&gt;Place documentation for the 'TestSpringConfig' process here.&lt;/documentation&gt;
      &lt;startEvent id="startevent1" name="Start"&gt;&lt;/startEvent&gt;
      &lt;serviceTask id="servicetask1" name="BeanTest" activiti:class="org.bpmn.examples.GetBeanTest"&gt;&lt;/serviceTask&gt;
      &lt;endEvent id="endevent1" name="End"&gt;&lt;/endEvent&gt;
      &lt;sequenceFlow id="flow1" name="" sourceRef="startevent1" targetRef="servicetask1"&gt;&lt;/sequenceFlow&gt;
      &lt;sequenceFlow id="flow2" name="" sourceRef="servicetask1" targetRef="endevent1"&gt;&lt;/sequenceFlow&gt;
    &lt;/process&gt;
  &lt;/definitions&gt;
  </code></pre>
  
  <p>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?</p>
  
  <p>Any/All replies appreciated!</p>
  
  <hr>
  
  <p>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. </p>
  
  <p>I change the xml file to look like this:</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.xsd"&gt;
  
      &lt;bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource"&gt;
          &lt;property name="driverClass" value="org.h2.Driver" /&gt;
          &lt;property name="url" value="jdbc:h2:tcp://localhost/activiti" /&gt;
          &lt;property name="username" value="sa" /&gt;
          &lt;property name="password" value="" /&gt;
      &lt;/bean&gt;
  
    &lt;bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration"/&gt;
    &lt;bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean"&gt;
      &lt;property name="processEngineConfiguration" ref="processEngineConfiguration" /&gt;
    &lt;/bean&gt;
  
    &lt;bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"&gt;
      &lt;property name="dataSource" ref="dataSource" /&gt;
    &lt;/bean&gt;
  
    &lt;bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration"&gt;
      &lt;property name="dataSource" ref="dataSource" /&gt;
      &lt;property name="transactionManager" ref="transactionManager" /&gt;
      &lt;property name="databaseSchemaUpdate" value="true" /&gt;
      &lt;property name="jobExecutorActivate" value="false" /&gt;
    &lt;/bean&gt;
  
    &lt;bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean"&gt;
      &lt;property name="processEngineConfiguration" ref="processEngineConfiguration" /&gt;
    &lt;/bean&gt;
  
    &lt;bean id="repositoryService" factory-bean="processEngine" factory-method="getRepositoryService" /&gt;
    &lt;bean id="runtimeService" factory-bean="processEngine" factory-method="getRuntimeService" /&gt;
    &lt;bean id="taskService" factory-bean="processEngine" factory-method="getTaskService" /&gt;
    &lt;bean id="historyService" factory-bean="processEngine" factory-method="getHistoryService" /&gt;
    &lt;bean id="managementService" factory-bean="processEngine" factory-method="getManagementService" /&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>When I restarted Tomcat no exceptions appear, however when I bring up Explorer and try to login, I get the following Exception: </p>
  
  <pre><code>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)
  </code></pre>