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.