NullPointer with Spring @Autowired fields within a JavaDelegate

1,021 views
Skip to first unread message

Melissa Palmer

unread,
Feb 13, 2015, 7:29:20 AM2/13/15
to camunda-...@googlegroups.com
Hi 

I don't seem to be able to user @Autowired fields within a JavaDelegate when specifying the class to be used for a ServiceTask as the delegateExpression 

My code for the JavaDelegate is something simlar to 
public class TestDelegate implements JavaDelegate {

    @Autowired
    private TestObjectAutowire autoWireMeIntoNotification;
 
    public void execute(DelegateExecution execution) throws Exception {
        System.out.println("autoWireMeIntoNotification:: " + autoWireMeIntoNotification);

        System.out.println("autoWireMeIntoNotification.toLowerCase():: " + autoWireMeIntoNotification.toLowerCase()); 
    }  
}

Within the Spring application context I have 
<bean id="testDelegate" class="za.co.mycompany.services.TestDelegate" />

<bean id="autoWireMeIntoNotification" class="za.co.itdynamics.bpm.service.TestObjectAutowire">
    <property name="value" value="setting this from the xml" />
</bean>

And the class TestObjectAutowire is as follows: 
public class TestObjectAutowire {
    private String value = null;

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public String toLowerCase() {
        // TODO Auto-generated method stub
        return value.toLowerCase();
    }
}


And finally within the flow on a ServiceTask I use as follows: 
<bpmn2:serviceTask id="JER_CAPTURED_NOTIFICATION" camunda:delegateExpression="${testDelegate}" name="Confirm Journal Entry Captured Notification">
   ...      
   ... 
</bpmn2:serviceTask>


 
My issue is that the autoWireMeIntoNotification field is always Null .... what am I doing wrong? 
    
Thanks in advance 
Melissa

Melissa Palmer

unread,
Feb 13, 2015, 8:35:44 AM2/13/15
to camunda-...@googlegroups.com
PS: there is one more important thing I have done which.. might be my problem... however I'm still not sure how to get around it. 
All of my Java Classes such as TestDelegate, and TestObjectAutowire are actually held within a jar file. I have a baseSpringApplicationContext.xml file which contains the information described below. 

Within my process war I have the Spring applicationContext.xml under \src\main\webapp\WEB-INF    which looks as follows: 
                         http://www.springframework.org/schema/context

<import resource="classpath:baseSpringApplicationContext.xml" />
</beans>


and my web.xml is:
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">

  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
  </context-param>

  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

</web-app>




Thanks
Melissa

thorben....@camunda.com

unread,
Feb 18, 2015, 8:27:52 AM2/18/15
to camunda-...@googlegroups.com
Hi Melissa,

so here is my theory on why @Autowired doesn't work:
You have the spring libraries in both, global classpath and webapp classpath. Your delegate implementation is in the global classpath. When this class is loaded, it uses the global classloader. This classloader will also resolve the @Autowired annotation. It therefore loads @Autowired from the global library folder. When you now bootstrap an application context in your webapp, it uses the Spring classes in the webapp's lib folder. That means, it also uses a different @Autowired class and therefore won't detect the annotation in the delegate.

Cheers,
Thorben
Reply all
Reply to author
Forward
0 new messages